0

I would like to pass two parameters to my url(status code & parent id). The json response of the url request is such :

{
"page": 1,
"per_page": 10,
"total": 35,
"total_pages": 4,
"data": [
       {
        "id": 11,
        "timestamp": 1565193225660,
        "status": "RUNNING",
        "operatingParams": {
            "rotorSpeed": 2363,
            "slack": 63.07,
            "rootThreshold": 0
        },
        "asset": {
            "id": 4,
            "alias": "Secondary Rotor"
        },
        "parent": {
            "id": 2,
            "alias": "Main Rotor Shaft"
        }
    }]

I would like to know how to pass the two parameters in the url. Passing ?status=RUNNING gives the response of all the devices which have running as status (thats pretty straightforward). For now I have tried this:

import requests
resp = requests.get('https://jsonmock.hackerrank.com/api/iot_devices/search?status=RUNNING')
q = resp.json()
print(q)

How should I pass in parentid=2, so it returns a response with devices which have their parent id=2.Thank you.

3
  • HTTP request libraries like requests have well-documented ways to construct query strings for URLs; are you using one? The response isn't of any use to us for describing how to make the request in the first place. Commented Jun 26, 2020 at 14:22
  • If the API you are making a request to accepts the parentid parameter, you can simply pass it as a second query string parameter: https://jsonmock.hackerrank.com/api/iot_devices/search?status=RUNNING&parentid=2. Commented Jun 26, 2020 at 14:32
  • Well tried that as well, but its not accepting Commented Jun 26, 2020 at 14:42

3 Answers 3

2

It's plainly documented under "Passing Parameters in URLs" in the Requests docs.

resp = requests.get(
    'https://jsonmock.hackerrank.com/api/iot_devices/search',
    params={
        'status': 'RUNNING',
        'parentid': 2,
    },
)
Sign up to request clarification or add additional context in comments.

Comments

0

To add a second get parameter, use the & separator :

import requests
resp = requests.get('https://jsonmock.hackerrank.com/api/iot_devices/search?status=RUNNING&parentid=2')
q = resp.json()
print(q)

1 Comment

@AKX answer is probably better as it leverage the requests api in a cleaner, more readable manner.
0

If you want to send data via get request the process is straight forward note how different values are seperated with '&'.

url?name1=value1&name2=value2

If you are using Flask for backend then you can access these parameters like.

para1=request.args.get("name1")
para2=request.args.get("name2")

On the front end you can use ajax to send the request

var xhttp=new XMLHttpRequest();
var url="url?name1=value1&name2=value2"
xhttp.open("GET",url,true)
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) 
  {
    console.log(this.responseText);
  }
};
xhttp.send();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.