Request Python form/data

Asked

Viewed 5,197 times

1

Guys I made this request in Python but not enough data in API, made in Postman works perfectly.

API_URL     = "https://api-us.faceplusplus.com/facepp/v3/detect"
    api_key     = "7Z...uC"
    api_secret  = "lh...Sg"
    image_url   = "http://pushsistemas.com.br/id2.jpg"
    headers = {
    'content-type': "multipart/form-data"
    }
    data = {
        "api_key":api_key,
        "api_secret":api_secret,
        "image_url":image_url
    }

    request_url = API_URL
    face_response = requests.post(
        request_url,
        data = data,
        headers=headers
    )


    face_response = face_response.json()
    print(face_response)

Postman

import requests

url = "https://api-us.faceplusplus.com/facepp/v3/detect"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"api_key\"\r\n\r\n7Z...uC\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"api_secret\"\r\n\r\nl...Sg\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"image_url\"\r\n\r\nhttp://pushsistemas.com.br/id2.jpg\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'cache-control': "no-cache",
    'Postman-Token': "0c381b9f-58c7-47da-91a8-c98670c0e2f9"
    }

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

print(response.text)
  • 1

    And what happens? Error? Something?

  • yes tell that the parameters did not arrive in the API.

  • I updated the code the return of the api is that the parameters are missing

  • 1

    Why there is "multipart/form-data" in data? It wouldn’t just be "api_key": api_key?

  • Yes, but it doesn’t work that way

  • If yes, then take it out. Export the Postman request that worked and post in the question.

  • I put the Postman output

Show 2 more comments

1 answer

2


You are setting the header Content-Type as multipart/form-data, but you do not send the data in this way. Do not complicate what is simple.

Just inform the data via data, headroom:

face_response = requests.post(
    request_url,
    data = {
        "api_key": api_key,
        "api_secret": api_secret,
        "image_url": image_url
    }
)

And done, your request will be successfully completed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.