1

I am having trouble using Requests

The API I am testing indicates the parameter device_info to be POSTED in the message body. It also says that the device_info as a form field. In all the documentation for Requests I cannot find how to add a "name" to the parameter other than slap the name with it's value in json. Here is what I have tried.

import requests
import json

loginPayload = {'device_info':{'app-id':'fc','os-type':'ios'}}
loginHeaders = {'content-type': 'application/json','Authorization':'Basic base64here'}
loginUrl = "http://subdomain.test.com/endpoint/method"
loginPost = requests.post(loginUrl, params=json.dumps(loginPayload), headers=loginHeaders)

print loginPost.text

I have tried changing params= to data= but I've had no luck.

The response back I'm getting is:

{
"response": {
"message": "Parameter 'device_info' has invalid value ()", 
"code": 400, 
"id": "8c4c51e4-9db6-4128-ad1c-31f870654374"
  }
}

EDITED:

Getting somewhere new! I have no modified my code to look as follows:

import requests

login = 'test'
password = 'testtest'
url = "http://subdomain.domain.com/endpoint/method"

authentication = (login,password)
payload = {'device_info': {'device_id': 'id01'}}
request = requests.post(url, data=payload, auth=authentication)

print request.text

Which produces:

{
  "response": {
    "message": "Parameter 'device_info' has invalid value (device_id)", 
    "code": 400, 
    "id": "e2f3c679-5fca-4126-8584-0a0eb64f0db7"
  }
}

What seems to be the the issue? Am I not submitting it in the required format?

EDITED: The solution was changing my parameters to:

{
    "device_info": "{\"app-id\":\"fc\",\"os-type\":\"ios\",\"device_id\":\"myDeviceID1\"}"
}
4
  • Try sending your content-type as ' application/json'. (with the space). just a wild guess because once I faced a similar issue. Commented May 6, 2013 at 21:00
  • Thanks, but nope. I tried it with the space and no go. I know the encoding that is required is 'application/x-www-form-urlencoded' Commented May 6, 2013 at 21:04
  • In that case, did you double check with the documentation of the API that you are using? Maybe, it could be an internal thing. Btw, the loginPayload in your question is missing a }. Commented May 6, 2013 at 21:14
  • do you know if that is the correct to add a name to the parameter? Commented May 6, 2013 at 23:26

1 Answer 1

2

So there are a few issues here:

  • You don't say that the website you're posting to requires JSON data, in fact in your comment you say that "the encoding that is required is 'application/x-www-form-urlencoded'.
  • params refers to the parameters of the query string. What you need is the data parameter.

So if your application is looking for 'application/x-www-form-urlencoded' data you should NOT be:

  • Setting the Content-Type header
  • Using json.dumps on your payload data

What you should do is the following:

import requests

login_payload = {'device_info': {'app-id': 'fc', 'os-type': 'os'}}
authentication = (login, password)  # Anyone who sees your authorization will be able to get this anyway
url = 'http://example.com/login'
response = requests.post(url, data=login_payload, auth=authentication)

I don't know of a RESTful API that takes x-www-form-urlencoded data, but you could also be describing your issue incorrectly. You haven't given us much to go on and you haven't given me more than the ability to guess. As such, this is my absolute best guess based on everything else you said.

Sign up to request clarification or add additional context in comments.

1 Comment

It was definitely removing the headers piece in favor of the authentication piece, almost there! Thank you for your valiant guess!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.