itsPav
itsPav

Reputation: 612

Python netsuite request parameters

I have the following base URL

https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder

Works fine to give me all the sales orders. However, when I add the following query params

?q=createdDate AFTER "01/01/2022"

It says invalid URL, no schema supplied. Works fine in postman though.

I tried having that query param exactly in the url like so;

https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder?q=createdDate AFTER "01/01/2022"

Also tried having it as a params in requests, but this gives me an auth token error.

headers = {token_info,private_keys}
params = {'createdDate': 'AFTER "01/01/2022"'}
payload = {}

requests.request(HTTP_METHOD, BASE_URL, params=params, headers=headers, data=payload)

Wondering what else I could try.

Upvotes: 0

Views: 833

Answers (1)

radekholy24
radekholy24

Reputation: 417

The trouble here is the special characters in the query parameter. They need to be properly encoded. Here is a discussion on how to encode space characters: URL encoding the space character: + or %20? The / characters need to be encoded as %2F sequences and " as %22.

I have done a little experiment with the requests library and it seems that it expects url arguments to be properly encoded already:

>>> BASE_URL = 'http://localhost:8000?q=createdDate AFTER "01/01/2022"'
>>> params = None
>>> requests.request(HTTP_METHOD, BASE_URL, params=params, headers=headers, data=payload)
127.0.0.1 - - [22/Apr/2022 07:14:25] "GET /?q=createdDate%20AFTER%20%2201/01/2022%22 HTTP/1.1" 200 100

As you can see, for some reason, it encodes the space character as a %20 sequence, whilst you need it to be encoded as a + character, and the / character is not encoded at all.

In order to fix that you would have to provide it the following BASE_URL:

>>> BASE_URL = 'https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder?q=createdDate+AFTER+%2201%2F01%2F2022%22'
>>> requests.request(HTTP_METHOD, BASE_URL, params=params, headers=headers, data=payload)
127.0.0.1 - - [22/Apr/2022 07:40:52] "GET /?q=createdDate+AFTER+%2201%2F01%2F2022%22 HTTP/1.1" 200 100

There are some libraries available that can properly encode the URL for you, but as you have already found out, requests can do that as well (and Postman too). All you need is to pass the query parameters as a params argument.

But there is a little mistake in your code snippet. You pass a query parameter with a key createdDate and a value AFTER "01/01/2022":

>>> BASE_URL = 'http://localhost:8000'
>>> params = {'createdDate': 'AFTER "01/01/2022"'}
>>> requests.request(HTTP_METHOD, BASE_URL, params=params, headers=headers, data=payload)
127.0.0.1 - - [22/Apr/2022 07:48:27] "GET /?createdDate=AFTER+%2201%2F01%2F2022%22 HTTP/1.1" 200 100

Notice the missing q= there. You probably need to pass a query parameter with a key q and a value createdDate AFTER "01/01/2022":

>>> BASE_URL = 'https://ACCOUNT_ID.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder'
>>> params = {'q': 'createdDate AFTER "01/01/2022"'}
127.0.0.1 - - [22/Apr/2022 07:50:20] "GET /?q=createdDate+AFTER+%2201%2F01%2F2022%22 HTTP/1.1" 200 100

Upvotes: 1

Related Questions