0

I have this code in python to make requests:

import requests
import json
import csv
import pandas as pd

with open('C:/asg/ftpfiles/data.json') as json_file:
    dataparse = json.load(json_file)

iPartNumber = dataparse['distributornumsku']

  

r = requests.get('https://online.xxx.com/xxx/items/' + str(iPartNumber), auth=('test@test.com', 'test')).json()
with open('itemdatasku.json', 'w') as json_file:
    json.dump(r, json_file)

The data JSON I’m reading is:

{"distributornumsku": "RFBB5-56C40K2/32"}

However the script crashes with the error:

Traceback (most recent call last):
  File "C:\asg\ftpfiles\api.py", line 28, in <module>
    r = requests.get('https://online.xxx.com/xxx/items/' + str(iPartNumber), auth=('test@test.com', 'test')).json()
  File "C:\Users\serverqnapwin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The error was because I’m sending the value RFBB5-56C40K2/32 and there is the problem maybe the “/“ or who knows is causing the problem/ If I use any other value offered than RFBB5-56C40K2/32 it works.

The problem is the code is sending incorrectly the request at difference of other cases for example. The script is sending:

https://online.xxx.com/xxx/items/RFBB5-56C40K2/32

As you see the last “/32” is the problem is not sending as a whole “RFBB5-56C40K2/32”. How can I avoid the /32 that it splits after the “/“ as if was another subdirectory after the “/“?

What could be the problem and how can I fix the script to avoid this error?

alex1290
  • 17
  • 4
  • Are you sure that's what the API is returning? Try using `.contents()` to see the actual value returned by the API. – Barmar Mar 22 '23 at 16:45
  • I detected the problem. I updated the main question the problem is that the “/32” at the end of the element. The “32” is taken as a subdirectory. That is the problem and I don’t know how to solve. – alex1290 Mar 22 '23 at 17:07
  • The slash isn't being added by the script, it's in `iPartNumber`. What is the correct URL? – Barmar Mar 22 '23 at 18:18
  • If you want to remove `/32`, use `iPartNumber.split('/')[0]` – Barmar Mar 22 '23 at 18:21
  • The problem is we need to search RFBB5-56C40K2/32 complete if I remove the /32 is incorrect the search. I just wanna know how to send the www and that includes the /32 – alex1290 Mar 22 '23 at 20:40
  • I don't understand. If that's what you need to search, why do you say that `/32` is the problem? – Barmar Mar 22 '23 at 20:42
  • Oh is simple. Because, the link to request with get is “ https://online.xxx.com/xxx/items/RFBB5-56C40K2/32” as you can see check at the end the ‘/32’ instead to search whole by logical is searching in subdirectory 32. This is the link ‘https://online.xxx.com/xxx/items/RFBB5-56C40K2/’ and it searches for 32 only that is the problem – alex1290 Mar 22 '23 at 21:13
  • It must search “https://online.xxx.com/xxx/items/RFBB5-56C40K2/32“ Literally – alex1290 Mar 22 '23 at 21:14
  • I'm totally lost. I can't see the distinction you're making. HTTP doesn't have directories and subdirectories. The server can interpret the URL however it wants. – Barmar Mar 22 '23 at 21:17
  • Either the URL should end with `/32` or not. Which do you want? If you want to search for `32` but not put `/32` in the link, where is it supposed to go instead? – Barmar Mar 22 '23 at 21:18
  • Put backticks around the URL in comments so it won't format it as a clickable link. – Barmar Mar 22 '23 at 21:19
  • ‘`https://online.xxx.com/xxx/items/RFBB5-56C40K2/32`’ What it does this is search – alex1290 Mar 22 '23 at 21:30
  • That's the URL you're sending with `requests.get()`. What should it be instead? – Barmar Mar 22 '23 at 21:32
  • The web page ‘ https://online.xxx.com/xxx/items/ ‘ must accept the request to search for item ‘RFBB5-56C40K2/32’ apparently the error is it takes /32 as a subdirectory of the server and is not searching whole ‘RFBB5-56C40K2/32‘. It truncates the search as i searching for a directory ‘32’ inside the server that obvious it doesn’t exist – alex1290 Mar 22 '23 at 22:43
  • Maybe you're supposed to encode the `/` character somehow if it's part of a search string. This would be something specific to that website. Or maybe you can use `%2F` in the URL. – Barmar Mar 22 '23 at 22:45
  • See https://stackoverflow.com/questions/5607551/how-to-urlencode-a-querystring-in-python for how to encode it. – Barmar Mar 22 '23 at 22:46
  • I solved the issue. The %2F not worked. But what worked is send the value like this ‘ https://online.xxx.com/xxx?items=RFBB5-56C40K2/32 ‘ By any reason the server accepted the ‘?items=‘ just for the values with ‘/‘ for the values without ‘/‘ no need to enter ‘?items=‘. Then is solved now with ‘?items=“. Thank you anyway. – alex1290 Mar 23 '23 at 00:13
  • It's for exactly the reason you said. In the path part of a URL, `/` is a separator. But it has no special meaning in the query string. – Barmar Mar 23 '23 at 00:13

0 Answers0