1

I got a local API that I want to test with very basic POST-requests. The PowerShell test-script works perfectly fine but the Python test-script (that should work the same way) does not.


PowerShell: api_post.ps1

$url = "http://test.local/"

$headers = @{"Content-Type" = "application/json"}

$payload = @(
    @{
        "Order_Number" = "123-vfs"
        "SKU"          = 123
        "Company"      = "Test Ltd"
    }
)

$payload = ConvertTo-Json -InputObject $payload

# Works exactly as it should
Invoke-RestMethod -Uri $url -Method "POST" -Headers $headers -Body $payload

Python api_post.py

import json
import requests

URL = "http://test.local/"

HEADERS = {
    "Content-Type": "application/json"
}

PAYLOAD = [
    {
        "Order_Number": "123-vfs",
        "SKU": 123,
        "Company": "Test Ltd",
    }
]

# Returns an error
requests.post(URL, headers=HEADERS, data=json.dumps(PAYLOAD))

The returned error of the API is not meaningful since the API is still in the early testphase

P.S: I didn't tag PowerShell here since the PowerShell-example only shows that the POST generally works

totooooo
  • 1,050
  • 1
  • 12
  • 32
Razorfen
  • 423
  • 4
  • 12

1 Answers1

1

requests.post's data argument doesn't take a string, you should pass it the dict directly :)

requests.post(URL, headers=HEADERS, data=PAYLOAD[0])

See the quickstart from requests on POST

totooooo
  • 1,050
  • 1
  • 12
  • 32
  • Thanks for the quick reply. Unfortunately I get an error if I pass the data directly. `ValueError: too many values to unpack (expected 2)` – Razorfen Mar 12 '19 at 14:32
  • @Razorfen ooh this is because your payload is a list I believe. Maybe try `data=PAYLOAD[0]`? – totooooo Mar 12 '19 at 14:34
  • I tried that - is results in the same error I got initially. I guess it's because the API needs the payload to be provied as an array/list. My PowerShell-script didn't succeed until I used `ConvertTo-Json` like above to create the JSON-array – Razorfen Mar 12 '19 at 14:37
  • 1
    did you try json=payload? A list is fine with json in Python. Also if you do that you don't need to specify the headers –  Mar 12 '19 at 14:47
  • @sit_on_a_pan_otis isn't `json=PAYLOAD` the same as `data=json.dumps(PAYLOAD)` ? – totooooo Mar 12 '19 at 14:49
  • Nope it's different... The only time I use data=.... is if I am posting xml. You can use either one though... I just like the json= .... did you try it? –  Mar 12 '19 at 14:50
  • 1
    also see here: https://stackoverflow.com/questions/9733638/post-json-using-python-requests/9952774 –  Mar 12 '19 at 14:54
  • json= will set the headers for you I believe... either way a list is fine and you shouldn't need to to specify the first an only element (the dict at [0]) –  Mar 12 '19 at 14:59
  • @sit_on_a_pan_otis It seems passing the `PAYLOAD`list directly to `data`raises a `ValueError`because requests tries to use the [payload_tuples syntax](http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests). But `json=PAYLOAD`should be fine indeed – totooooo Mar 12 '19 at 15:03
  • Thanks for the hint - unfortunately I still receive an error despite the `json=` approach. I'm starting to think that it may just be an error in the API and that the PowerShell-Script just works accidentally. I can't provide further details since I'm not involved in the API-development - sorry :-( – Razorfen Mar 12 '19 at 15:08
  • How are you receiving the data on the backend of the server? I am accustomed to Flask and Django.. maybe when you parse the incoming request on the local loopback server something needs to change in your code? –  Mar 12 '19 at 15:23
  • ie: in Flask.. the route that you request might get the incoming request like this: data = request.json .... Instead of manipulating that incoming data... maybe just try print(request.json) to see if your data is really getting to the server –  Mar 12 '19 at 15:27
  • @sit_on_a_pan_otis I don't develop the API, I'm just testing it on my end :) – Razorfen Mar 12 '19 at 15:28
  • the only thing I know that the API is implemented through lavarel – Razorfen Mar 12 '19 at 15:28