Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a string to a post call, using python requests

I'm implementing a jira api call : Add watcher to a JIRA issue.

It takes a String parameter instead of JSON.

I'm using python requests.

requests.post(url, headers=headers, json=data)

What should be my value of data if the jira documentation says I need to pass just String but requests.post method only accepts JSON?

like image 680
Ankit Chaudhari Avatar asked Apr 18 '17 18:04

Ankit Chaudhari


People also ask

How do you send data in a POST request in Python?

The post() method sends a POST request to the specified url. The post() method is used when you want to send some data to the server.

How do you pass parameters in POST request URL in Python?

To send parameters in URL, write all parameter key:value pairs to a dictionary and send them as params argument to any of the GET, POST, PUT, HEAD, DELETE or OPTIONS request. then https://somewebsite.com/?param1=value1&param2=value2 would be our final url.

Can we send parameters in POST request?

In a POST request, the parameters are sent as a body of the request, after the headers. To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.

How do you pass a body request in Python?

You'll want to adapt the data you send in the body of your request to the specified URL. Syntax: requests. post(url, data={key: value}, json={key: value}, headers={key:value}, args) *(data, json, headers parameters are optional.)


1 Answers

Simply do:

requests.post(url, headers=headers, data=data)

According to the official docs:

There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.

like image 200
Matheus Portela Avatar answered Oct 21 '22 21:10

Matheus Portela