Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post JSON dat and Image
#1
When i try to post json data and image in API link i get the error for image as unicode error

import pandas
import requests
import json
import base64


headers = {'Content-type':  'application/json'}

files={'file': open('C:\Users\vijsi-in\PycharmProjects\RDP Access Automation\Input Method - UI Path.JPG')}

with open("Input Method - UI Path.JPG","rb") as files:
    encoded_string = base64.b64decode((files.read()))

briefDescription = "Test"

data = {
  "briefDescription": briefDescription,
  "request" : "I am affected by this issue :-" + "\n" +
    "Yes More detailed information - Kindly I need to activate my office and when I try to activate it I get the attached error PC Number -Desk location",
    "status": "secondLine",
    "callerLookup" : {"networkLoginName" : "satna-in"},
     "category" : { "id" : "c8322080-94f2-4911-9b4d-527f7d2e7cac"},
  "subcategory": {"id" : "3d347c83-a0a2-4ea7-be4f-0c142ad03f97"},
  "callType": {"id" : "b46bd95d-1b4b-5667-bf6a-86531696c8cc"},
    "operatorGroup":{"id": "53f12042-4df0-409c-ab1b-a9c3c2889db4"},
    "entryType":{"id": "b330b233-7199-548c-8c47-8586190e3aa3"},
    "impact":{"id": "6dd9cf42-0529-52bc-bbee-87872646ac71"},
    "urgency":{"id": "7ba734d4-1aa4-4d93-b8de-9796f0845d28"},
    "entryType":{"id": "8413ecbd-6bb1-43df-b981-c06a267a2416"},
ERROR I GET:

Error:
File "C:/Users/vijsi-in/.PyCharmEdu2019.2/config/scratches/Incident Creation.py", line 11 files={'file': open('C:\Users\vijsi-in\PycharmProjects\RDP Access Automation\Input Method - UI Path.JPG')} ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Reply
#2
on line 9 you need to do one of the following:
use raw string, e.g. r'C:\Users\vijsi-in\PycharmProjects\RDP Access Automation\Input Method - UI Path.JPG'
use double back slash, e.g. 'C:\\Users\\vijsi-in\\PycharmProjects\\RDP Access Automation\\Input Method - UI Path.JPG'
use forward slash, e.g. 'C:/Users/vijsi-in/PycharmProjects/RDP Access Automation/Input Method - UI Path.JPG'

back slash in combination of some other chars (U in this case) represents escape sequence
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
When i try to post json data and image in API link i get the error for "Data must not be a string."

import pandas
import requests
import json
import codecs
import base64

auth_key = ('xxx', 'xxxxxxxxxxxxxxx')

URL1 = "https://fls.topdesk.net/tas/api/incidents/"
headers = {'Content-type': 'application/json','Content-type':'charset=utf-8'}

with open("C:\\Users\\vijsi-in\\PycharmProjects\\RDP Access Automation\\topdesk.png", "rb") as f:
    encodedZip = base64.b64encode(f.read())
    files = encodedZip.decode()
    print(files)

briefDescription = "Test"
  

data = {
  "briefDescription": briefDescription,
  "request" : "I am affected by this issue :-" + "\n" +
    "Yes More detailed information - Kindly I need to activate my office and when I try to activate it I get the attached error PC Number -Desk location",
  "status": "secondLine",
  "callerLookup" : {"networkLoginName" : "vijsi-in"},
  "category" : { "id" : "7faa6bf8-a30a-422f-a751-5873f23edb0c"},
  "subcategory": {"id" : "0ed51db1-5121-4808-830e-5bc14046f4fc"},
  "callType": {"id" : "91f897c3-617c-4577-b8ca-2a6b02ff527c"},
  "operatorGroup":{"id": "53ba321f-2b61-4c62-93f3-c1cb2c928d36"},
  "entryType":{"id": "b330b233-7199-548c-8c47-8586190e3aa3"},
  "impact":{"id": "6dd9cf42-0529-52bc-bbee-87872646ac71"},
  "urgency":{"id": "7ba734d4-1aa4-4d93-b8de-9796f0845d28"},
  "entryType":{"id": "b9743d23-5338-4910-9ac0-87f61a502a90"},
   }


data_json = json.dumps(data)

r = requests.post(URL1,data=data_json,files=files,headers=headers,auth = auth_key)
r.status_code
a=r.content
print(r.status_code)
#print(a)
c=(a[77:91])
print(c)
Result:
ValueError: Data must not be a string.
for line number:39
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add an image to an existing facebook post using python graph API? Ascalon 0 2,180 Dec-01-2020, 08:33 PM
Last Post: Ascalon
  Post JSON from python to PHP don't give expected result pgagnebin 1 3,691 Sep-04-2019, 10:29 PM
Last Post: micseydel
  Help with passing command line input to HTTP POST json sf05wrx 2 2,381 Feb-25-2019, 11:19 PM
Last Post: sf05wrx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020