user1555608
user1555608

Reputation: 5

PUT Python requests is not updating data

Edit update:

I tried the solution given on this page, but that does not work for me either: Put request working in curl, but not in Python

I am using python 2.7. I am trying to use a PUT request. Data has to be sent as formdata.

import requests
import json
url = "http://httpbin.org/put"
data = {'lastName':'testNew'}
headers = {

    'Authorization': "JWTeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_FkSW5s",
    }

response = requests.request("PUT", url,data=data, headers=headers)

print(response.text)

This requests gives me the following response:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "lastName": "testNew"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_", 
    "Connection": "close", 
    "Content-Length": "16", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "json": null, 
  "origin": "111.93.35.2", 
  "url": "http://httpbin.org/put"
}

But when I put the api url where I want to use this request, api hangs and gives bad connection error.

The API where I am trying to update the values works fine with Postman, and the code they give in clipboard is as following and it works like a charm updates the data:

import requests

url = "http://myUrl/dashboard/editProfile"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"lastName\"\r\n\r\nChangeLastFromCode\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'Authorization': "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0LCJlbWFpbCI6ImNlbGVzdGlhbHRlc3QxQGdtYWlsLmNvbSIsInJhbmRvbV9qd3QiOiJlNXg1Q0oifQ.xc5jVAS6ZtTPrjg0LizznT0-sE9W_",

    }

response = requests.request("PUT", url, data=payload, headers=headers)

print(response.text)

How do I write the query without having to use boundary value as given in Postman example above ? It's only a simple PUT request in form data. Values can be one or more. In the above example, I am only using 'lastName' which I want to change via PUT>

Upvotes: 0

Views: 4010

Answers (1)

user10333474
user10333474

Reputation: 26

Best way to handle boundary condition is multipartencoder. https://toolbelt.readthedocs.io/en/latest/uploading-data.html

You can try something like following code.

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

m = MultipartEncoder(
    fields={'field0': 'value', 'field1': 'value',
            'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
    )

r = requests.post('http://httpbin.org/post', data=m,
                  headers={'Content-Type': m.content_type})

Upvotes: 1

Related Questions