New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying content type in PUT requests? #19

Closed
cboettig opened this Issue Aug 30, 2012 · 11 comments

Comments

Projects
None yet
2 participants
@cboettig
Contributor

cboettig commented Aug 30, 2012

I'm probably doing something wrong here, but it's not clear how I specify content requests in PUT.
I'm trying to access the Figshare API with httr.

Authentication with httr and oauth 1 seems to work fine:

figshare_auth <-  function(cKey = getOption("FigshareKey", stop("Missing Figshare consumer key")),
       cSecret = getOption("FigsharePrivateKey", stop("Missing Figshare app secret")),
       token = getOption("FigshareToken", stop("Missing Figshare token")),
       token_secret = getOption("FigsharePrivateToken", stop("Missing Figshare Secret Token"))){
  require(httr)
  myapp <- oauth_app("rfigshare", key = cKey, secret=cSecret)
  sig <-  httr:::sign_ouath1.0(myapp, token = token, token_secret = token_secret) 
  } 

session <- figshare_auth()

And most of the basic GET commands work. But I try to a post method and get:

figshare_create <- 
function(title="test", description="description of test", type="dataset", session){
  require(RJSONIO)
  base <- "http://api.figshare.com/v1"
  method <- paste("my_data/articles")
  request <- paste(base, method, sep="/")
  meta <- toJSON(list("title"=title, "description"=description,
                      "type"=type, "Content-Type" = "application/json"))
  POST(request, config=session, body=meta)
}

and get the error

figshare_create(session=session)
Response [http://api.figshare.com/v1/my_data/articles]
  Status: 401
{"error": "Incorect content_type. Expected application/json, received:"} 

The GET -based methods work fine (those calls also require the 'session' variable, but I guess this could still be an authentication issue since they shouldn't actually need to authenticate since those should be public methods)?

Any ideas? (I'm building out API in rfigshare package here: https://github.com/ropensci/rfigshare)

@hadley

This comment has been minimized.

Member

hadley commented Aug 30, 2012

I'd think you'd want:

  meta <- toJSON(list("title"=title, "description"=description, "type"=type))
  POST(request, config=session, body=meta, add_headers("Content-Type" = "application/json"))
@cboettig

This comment has been minimized.

Contributor

cboettig commented Aug 30, 2012

Ah, good idea. Unfortunately I'm getting the exact same error with this. Is there a way to see the literal POST request for debug purposes?

@hadley

This comment has been minimized.

Member

hadley commented Aug 30, 2012

The put request, you mean? Just add the verbose() config:

with_config(verbose(), figshare_create(...)
@cboettig

This comment has been minimized.

Contributor

cboettig commented Aug 30, 2012

hmm, I get this


< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 401 Unauthorized
< Server: nginx/0.8.54
< Date: Thu, 30 Aug 2012 16:32:53 GMT
< Content-Type: application/json; charset=UTF-8
< Connection: keep-alive
< Content-Length: 72
< 
* Connection #0 to host api.figshare.com left intact
Response [http://api.figshare.com/v1/my_data/articles]
  Status: 401
{"error": "Incorect content_type. Expected application/json, received:"} 
> 

Looks to me that it's actually the authentication that failed? ("401 Unauthorized"). The same keys work fine when I try the ruby client not sure where I have gone wrong. Thanks for the advice

@hadley

This comment has been minimized.

Member

hadley commented Aug 30, 2012

Is that the whole output? That's only what the server is sending back to you, not what you're sending to the server.

Otherwise, can you find the source code for that ruby method? It'll help to understand exactly what it's sending.

@cboettig

This comment has been minimized.

Contributor

cboettig commented Aug 30, 2012

I left out the top of the output that has the keys, can email that to you.

Here's the entire ruby command (keys scubbed) which works fine

require 'oauth'
require 'json'

key = "qMXXXXXXXXXXXXXXX"
secret = "zQXXXXXXXXXXXXXX"
token = "SrXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
secret_token = "yqIXXXXXXXXXX"


consumer = OAuth::Consumer.new(key, secret, {:site=>"http://api.figshare.com"})
token = { :oauth_token => token,
            :oauth_token_secret => secret_token 
        }
        client = OAuth::AccessToken.from_hash(consumer, token)

        body = JSON.generate('title'=>'Test dataset', 'description'=>'Test description', 'defined_type'=>'dataset')
        result = client.post('/v1/my_data/articles', body, {"Content-Type"=>"application/json"})

        print result.body

(from here: http://api.figshare.com/docs/demo_ruby.html#sample-code-for-ruby)

@hadley

This comment has been minimized.

Member

hadley commented Aug 30, 2012

Hmmm, that looks equivalent to your R code. Can you email me the full output?

@hadley

This comment has been minimized.

Member

hadley commented Aug 31, 2012

The problem is this line in make_request:

    config <- c(config, signed$config)

because c.config doesn't have any logic for combining headers.

@cboettig

This comment has been minimized.

Contributor

cboettig commented Aug 31, 2012

Nice work, thanks. Staying tuned for the update and thanks again for all the help.

@cboettig

This comment has been minimized.

Contributor

cboettig commented Sep 3, 2012

Any thoughts on concatenating headers?

@hadley hadley closed this in 4c7c34f Sep 4, 2012

@cboettig

This comment has been minimized.

Contributor

cboettig commented Sep 6, 2012

Fantastic! you rock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment