Anas Laghouaouta
Anas Laghouaouta

Reputation: 105

convert nested json to dict

Im trying to send the Json below with Scrapy

{
"version": 1,
"message_type": 104,
"message_id": 14,
"body": [
{
  "message_type": 104,
  "chat_message": {
    "mssg": "hello",
    "message_type": 1,
    "uid": "15373703487091",
    "from_person_id": "5134266921",
    "to_person_id": "3093543561",
    "read": false
  }
}
],
"is_background": false
}

I've tried to send it hard coded like this

self.postRequest = {"version":"1",
                    "message_type":"104",
                    "message_id":"18",
                    "body":"[{'message_type':'104','chat_message':{'mssg':'hello','message_type':'1','uid':'15372201045381','from_person_id':'5134266921','to_person_id':'3093543561','read':'false'}}]",
                        "is_background":"false"}

yield FormRequest(url=response.url   , formdata=self.postRequest, callback=self.parse_data,dont_filter=True, headers=self.params, cookies=self.cookies)

i get HTTP 200 status with an error message : Unknown command server_unknown_action.

so i dont know if i did something wrong with the json structure or the error is elsewhere

Upvotes: 0

Views: 228

Answers (1)

nyov
nyov

Reputation: 1462

The FormRequest class is for sending data as Content-Type: application/x-www-form-urlencoded. Sending JSON as a POST body likely means you really want to send Content-Type: application/json.

For this, use the normal Request class, with method set to POST, and json.dumps() your self.postRequest-data into the Request().body.

yield Request(url=response.url,
              method='POST',
              headers={
                  'Content-Type': 'application/json; charset=UTF-8',
              },
              body=json.dumps(self.postRequest),
              callback=self.parse_data,
              dont_filter=True,
              cookies=self.cookies)

Upvotes: 1

Related Questions