504 error with POST request for Python

Source: Internet
Author: User

This is a wonderful problem, I am also a wonderful study for several days, and finally found that, ha, the original is the reason, in this record the twists and turns of the mental process

Interface Content-type No, the body is using raw data in postman, the format is text

The procedure is as follows:

ImportRequestsurl="Https://XXXXXX.com/api/user_device"Headers= {'UserName':'[email protected]',            'Accesstoken':'08CXXXXXXXT051AS0O*EVCGA'           ,'appversion':'XXX4.3.3',           'Phoneos':'IPhone X',           'Phonemodel':'IOS 11.1',           'Phoneid':'Phoneiduniqueid',           'Phonelanguage':'en'}data= {"Lastconnecttime": 1623876540,        "Mdeviceid":"123456a",        "Fwver":"Yedun1tjbu2yvbokwakqiqnmijxrm7s9",        "Hwver":"4funui7be4z5jjgwt598hzeyv0qkoqdy",        "DeviceType":"XX5"}r= Requests.post (URL, headers=headers, data=data)Print(Type (data))Print(R.status_code)#print (R.json ())

1. Start is because there is an error, probably sslverify fail a fault, and then I find the reasons for various, the final solution is to add a parameter in the Requests.post verify=false; so the error becomes a warning, and then the program runs the report 504 This is the beginning of trouble!

Mind: I began to think that the SSL warning caused by the error 504, and then the various checks, and later found it seems wrong, and then study is not the body format caused by the problem

2. The various check body format and content-type format, there is really a harvest, Content-tpye has 4 formats: Application/x-www-form-urlencoded;multipart/form-data ; application/json;text/xml; The first three formats need to be in the header with the Content-type type, the last one is not required; raw belongs to the last, and it's in text format, so I'm in headers Riga.

' Content-type ': ' Text/xml '

Result: No more 504. Interface ran through, but the API reported a parameter error

Mind: There is no content-type in the agreement. may not be a mess.

Note: When trying various Content-type, some will report

3. Finally, I processed the data to solve the problem successfully. Data= json.dumps (data), and then the request is sent, Content-type plus (plus better).

Summary: Requests send request data needs to be STR format, and I write the JSON format, that is, Python dict, need to convert to use, but if only the newspaper wrong, do not know what the reason

Successful programs:

ImportRequestsImportJsonurl="Https://XXXXX.com/proapi/user_device"Headers= {'Content-type':'Text/xml',            'UserName':'[email protected]',            'Accesstoken':'08J*XXXXXKDT051AS0O*EVCGA'           ,'appversion':'XXX4.3.3',           'Phoneos':'IPhone X',           'Phonemodel':'IOS 11.1',           'Phoneid':'Phoneiduniqueid',           'Phonelanguage':'en'}data= {"Lastconnecttime": 1623876540,        "Mdeviceid":"123456a",        "Fwver":"Yedun1tjbu2yvbokwakqiqnmijxrm7s9",        "Hwver":"4funui7be4z5jjgwt598hzeyv0qkoqdy",        "DeviceType":"XX5"}data=json.dumps (data) R= Requests.post (URL, headers=headers, data=data)Print(R.status_code)#print (R.json ())

Episode: If HTTPS is an error (as long as it is not), print (Response.json) will error:

Raise Jsondecodeerror ("expecting value"from 1 column 1 (char 0)

Note: Python's request will default to application/x-www-form-urlencoded for data that does not give the Content-type type, but I don't know why I don't have a problem here; Urllib.request.AbstractHTTPHandler.do_request_ () in this file; If you don't want the default settings, you can comment out those sentences.

Major discoveries:

The original postman has a function of the interface request directly to the program, later writing program problems, you can reference

504 error with POST request for Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.