Anshul Sharma
Anshul Sharma

Reputation: 1123

Python http delete requests with URL path variable

I have an Http endpoint exposed as http://localhost:8080/test/api/v1/qc/{id} for delete, while making this API delete call I have to replace with the proper id

I tried below way using the requests module of python

param = {
  "id" : 1
}

requests.delete(url = http://localhost:8080/test/api/v1/qc/{id}, params=param)

This API call is breaking with the error

ValueError: No JSON object could be decoded.

How can I do this?

Upvotes: 0

Views: 3876

Answers (1)

Foo L
Foo L

Reputation: 11137

Your code can't run as-is. You need to quote your url string:

url = "http://localhost:8080/test/api/v1/qc/{id}"

Reading the docs for requests, the params only sends the dictionary param as the query string, so it'll only tack on ?id=1 to the end of the URL.

What you want is the {id} to get the value from the dictionary. You can look at this answer for various ways: How do I format a string using a dictionary in python-3.x?

You want something like

requests.delete(url = "http://localhost:8080/test/api/v1/qc/{id}".format(**param))

Upvotes: 5

Related Questions