-1

I have a file as follows:

{
    "http-request": {
        "header": {
            "method": "POST",
            "action": "register",
            "httpversion": "1.1",
            "host": "customerrequest.com",
            "Connection": "keepalive",
            "Content-Length": "254",
            "Origin": "https://clipboard.customerrequest.com",
            "User-Agent": "Chrome/56.0.2924.87 Safari/537.36"
        },
        "body": {
            "email": "jen@gmail.com",
            "password": "XXXXXXXX",
            "confirm_password": "XXXXXXXX",
            "invite_code": "",
            "csrf_token": "gshdgagTTDBsbasxgvdfjdkf-TI1kV42pAYhkFXQrKfvJjLYhpR-fJ2WjirVXaQ==",
        }
    }
}

I need to post this info to the server https://clipboard.customerrequest.com for server action register. I am a total noob with the whole http request response etc. I just need a little help here understanding

  1. Should I post the whole json as is to the server? If not, what is the best way to post this information to the server?

  2. When I get a response, I want to automatically parse the response and store the information.How can I convert the response body (http I think) to json format?

  3. I want to do this in python. Any resources/libraries I can look at?

user1357576
  • 389
  • 2
  • 3
  • 17
  • first of all is your server accepting header content type "Application/json". if yes you can post your data as it is to it – Alok Vishwakarma Mar 28 '17 at 05:35
  • @AlokVishwakarma I think they accept content type `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8` – user1357576 Mar 28 '17 at 05:41
  • Ooh there is no application/json tag, i think it should also accpet application/json other wise you can not post you data in json format. is that server belongs to you then you can change the accepted header. for now you you can only post your data as Parameters or as XML entity format – Alok Vishwakarma Mar 28 '17 at 05:55
  • @AlokVishwakarma What does post your data as parameters mean? Can I parse out the json values and create a request acceptable to the server? – user1357576 Mar 28 '17 at 05:56
  • Diffrence: if in my server side script i am only accepting header application/json then i will check for it, if incoming payload is in request content type having application/json then i will process the data, otherwise script will return false. – Alok Vishwakarma Mar 28 '17 at 06:00
  • please go thru both below image you will get better under standing for both parameters post request URL Image http://i67.tinypic.com/wu4v9.png Json post request image http://i65.tinypic.com/2b5gns.png – Alok Vishwakarma Mar 28 '17 at 06:12

1 Answers1

1

You can use libraries json to parse your file to get the json to be posted and then Request to post. You can get json from response.

  1. Use json to get the body as json from you file, something on below line would work

    import json with open('data.json') as data_file:
    data = json.load(data_file) payload = data["body"] url = data["headers"]["Origin"] //if url also needs to be extracted from file headers = {'content-type': 'application/json'}

  2. Use request to post the json

    r = requests.post(url, data=json.dumps(payload), headers=headers) resp = r.json()

There are multiple similar posts (1, 2 & 3) can also be referred,

Community
  • 1
  • 1
Anil
  • 3,722
  • 2
  • 24
  • 49
  • Interesting. What if the server doesn't accept `application/json` type content? – user1357576 Mar 28 '17 at 07:11
  • If a server has been configured to not to accept json content then why you are posting json, its very much possible server does not have capability to parse jason object also. From client we can not go beyond server capabilities. Refer https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16 for http response codes can enable you to decode the response you are getting. – Anil Mar 28 '17 at 07:24