Python dictionary converts single slash to double slash - json

I am trying to send data using requests trying to send the payload from my side which requires date to be formatted like "\/Date(1532010600000)\/"
I am trying to make a custom payload which is to be sent using requests:
payload = {
"GW_MeetingID": "1231324654654",
"AltID": "This is ALT ID",
"MeetingSubject": company,
"MeetingComment": "",
"RoomID": "xxxxx",
"TimeZoneId": "Dateline Standard Time",
"Organizer": organiser,
"Start": "\\/Date(1532010600000)\\/"
}
here the key should be "\/Date(1532010600000)\/" rather than "\\", using extra "\" as escape character. When i am trying to print this payload, it is showing value of start keys with "\\" which the api I am trying to send response to, doesn't accept. It only accepts 'Start' key as "\/Date(1532010600000)\/"
The solution that I have tried are using .encode('utf-8') and then decoding it using .decode('unicode_escape'). But still the dictionary had same response with "\\"
How can I make this payload to contain "\" in Start key?
import json
import requests
payload = {
"GW_MeetingID": "1231324654654",
"AltID": "This is ALT ID",
"MeetingSubject": company,
"MeetingComment": "",
"RoomID": "e3e63148-5e4f-426e-98de-dec1687c9930",
"TimeZoneId": "Dateline Standard Time",
"Organizer": organiser,
"Start": "\\/Date(1532010600000)\\/"
}
print (payload)
# print (json.dumps(payload))
# a = (ast.literal_eval(payload))
headers = {'Content-Type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print (r.text)

Related

trying to send JSON data over socket but and the reciving end is single quotes instead of double quotes

I am trying to send JSON data of python3 socket, but on the receiving end the double quotes get turned into single quotes so I can not read it
Sending
server_name = "One"
response_data = {"error": "false", "data": "Success", "Server": server_name}
response_request = "HTTP/1.1 200 OK\n\n" + response_data
sock.send(response_request.encode())
sock.close()
Receive
response = requests.get("http://localhost/")
print(response.text)
{'error': 'false', 'data': 'Success', 'Server': server_name}
The doubles quotes get changed to single quotes
But if I send
"""HTTP/1.1 200 OK\n\n{"error": "false", "data": "Success"}"""
It will not change the double quotes but I can not send the server_name variable
Your server doesn't work, but to make a working server you need to json.dumps the dictionary object into a string. Here's a minimal working JSON server that sets the content type header and can be read by the requests module:
server.py
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
class Server(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
server_name = "One"
response_data = {"error": "false", "data": "Success", "Server": server_name}
self.wfile.write(json.dumps(response_data).encode())
httpd = HTTPServer(('', 8080), Server)
httpd.serve_forever()
client.py
import requests
response = requests.get("http://localhost:8080/")
print(response.text) # returns string
print(response.json()) # returns parsed JSON object (dict in this case)
Output (client):
{"error": "false", "data": "Success", "Server": "One"}
{'error': 'false', 'data': 'Success', 'Server': 'One'}

Python Regex: How to match the string and then modify that string by adding something at the end

UPDATED CODE: It is working but now the problem is that the code is attaching same random_value to every Path.
Following is my code with a sample chunk of text. I want to read Path and it's value then add (/some unique random alphabet and number combination) at the end of every Path value without changing the already existed value. For example I want the Path to be like
"Path" : "already existed value/1A" e.t.c something like that.
I am unable to make the exact regex pattern of replacing it.
Any help would be appreciated.
It can be done by json parse but the requirement of the task is to do it via REGEX.
from io import StringIO
import re
import string
import random
reader = StringIO("""{
"Bounds": [
{
"HasClip": true,
"Lang": "no",
"Page": 0,
"Path": "//Document/Sect[2]/Aside/P",
"Text": "Potsdam, den 9. Juni 2021 ",
"TextSize": 12.0
}
],
},
{
"Bounds": [
{
"HasClip": true,
"Lang": "de",
"Page": 0,
"Path": "//Document/Sect[3]/P[4]",
"Text": "this is some text ",
"TextSize": 9.0,
}
],
}""")
def id_generator(size=3, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
text = reader.read()
random_value = id_generator()
pattern = r'"Path": "(.*?)"'
replacement = '"Path": "\\1/'+random_value+'"'
text = re.sub(pattern, replacement, text)
#This is working but it is only attaching one same random_value on every Path
print(text)
Use group 1 in the replacement:
replacement = '"Path": "\\1/1A"'
See live demo.
The replacement regex \1 puts back what was captured in group 1 of the match via (.*?).
Since you already have a json structure, maybe it would help to use the json module to parse it.
import json
myDict = json.loads("your json string / variable here")
# now myDict is a dictionary that you can use to loop/read/edit/modify and you can then export myDict as json.

how to reproduce 415 Unsupported Media type

How to reproduce 415 Unsupported media type?
I am trying with this with the upload to Google Drive.
Setup resumable upload so that I can add any media after the initial POST.
Thinking like during the first POST, the media type is not set completely. Google Drive will use some default option like application/octet-stream.
Then during the data file upload via PUT, will set the right media type.
If the PUT is Not success, the response should be 415
Second method could be by adding content-encoding during POST. If the encoding is not supported the response should be 415.
Here is Python code I rewrote from Web. How to change this to get 415?
Are there other methods to get this error? I am not familiar with Javascript, web resources are cryptic for me now.
import json
import os
import requests
access_token ="ya29.XXX.....XXXqI"
filename = './test-atom.xml'
filesize = os.path.getsize(filename)
# 1. Retrieve session for resumable upload.
headers = {"Authorization": "Bearer "+access_token, "Content-Type": "application/json"}
params = {
"name": "drive-small2.xml",
#"mimeType": "application/atom+xml" ## works without this line
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
headers=headers,
data=json.dumps(params)
)
location = r.headers['Location']
# 2. Upload the file.
#headers = {"Content-Type": "application/atom+xml", "Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)} ## works for this also
headers = { "Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)} ## works for this also
r = requests.put(
location,
headers=headers,
data=open(filename, 'rb')
)
print(r.text)
***********************
C:\Users\Administrator\Desktop\driveUpload>py resume-upload10.py
{
"kind": "drive#file",
"id": "1nQW6_-1F4fBEcKoln7vSM0qOTpWhSpZ2",
"name": "drive-small2.xml",
"mimeType": "text/xml"
}

Emit Python embedded object as native JSON in YAML document

I'm importing webservice tests from Excel and serialising them as YAML.
But taking advantage of YAML being a superset of JSON I'd like the request part of the test to be valid JSON, i.e. to have delimeters, quotes and commas.
This will allow us to cut and paste requests between the automated test suite and manual test tools (e.g. Postman.)
So here's how I'd like a test to look (simplified):
- properties:
METHOD: GET
TYPE: ADDRESS
Request URL: /addresses
testCaseId: TC2
request:
{
"unitTypeCode": "",
"unitNumber": "15",
"levelTypeCode": "L",
"roadNumber1": "810",
"roadName": "HAY",
"roadTypeCode": "ST",
"localityName": "PERTH",
"postcode": "6000",
"stateTerritoryCode": "WA"
}
In Python, my request object has a dict attribute called fields which is the part of the object to be serialised as JSON. This is what I tried:
import yaml
def request_presenter(dumper, request):
json_string = json.dumps(request.fields, indent=8)
return dumper.represent_str(json_string)
yaml.add_representer(Request, request_presenter)
test = Test(...including embedded request object)
serialised_test = yaml.dump(test)
I'm getting:
- properties:
METHOD: GET
TYPE: ADDRESS
Request URL: /addresses
testCaseId: TC2
request: "{
\"unitTypeCode\": \"\",\n
\"unitNumber\": \"15\",\n
\"levelTypeCode": \"L\",\n
\"roadNumber1\": \"810\",\n
\"roadName\": \"HAY\",\n
\"roadTypeCode\": \"ST\",\n
\"localityName\": \"PERTH\",\n
\"postcode\": \"6000\",\n
\"stateTerritoryCode\": \"WA\"\n
}"
...only worse because it's all on one line and has white space all over the place.
I tried using the | style for literal multi-line strings which helps with the line breaks and escaped quotes (it's more involved but this answer was helpful.) However, escaped or multiline, the result is still a string that will need to be parsed separately.
How can I stop PyYaml analysing the JSON block as a string and make it just accept a block of text as part of the emitted YAML? I'm guessing it's something to do with overriding the emitter but I could use some help. If possible I'd like to avoid post-processing the serialised test to achieve this.
Ok, so this was the solution I came up with. Generate the YAML with a placemarker ahead of time. The placemarker marks the place where the JSON should be inserted, and also defines the root-level indentation of the JSON block.
import os
import itertools
import json
def insert_json_in_yaml(pre_insert_yaml, key, obj_to_serialise):
marker = '%s: null' % key
marker_line = line_of_first_occurrence(pre_insert_yaml, marker)
marker_indent = string_indent(marker_line)
serialised = json.dumps(obj_to_serialise, indent=marker_indent + 4)
key_with_json = '%s: %s' % (key, serialised)
serialised_with_json = pre_insert_yaml.replace(marker, key_with_json)
return serialised_with_json
def line_of_first_occurrence(basestring, substring):
"""
return line number of first occurrence of substring
"""
lineno = lineno_of_first_occurrence(basestring, substring)
return basestring.split(os.linesep)[lineno]
def string_indent(s):
"""
return indentation of a string (no of spaces before a nonspace)
"""
spaces = ''.join(itertools.takewhile(lambda c: c == ' ', s))
return len(spaces)
def lineno_of_first_occurrence(basestring, substring):
"""
return line number of first occurrence of substring
"""
return basestring[:basestring.index(substring)].count(os.linesep)
embedded_object = {
"unitTypeCode": "",
"unitNumber": "15",
"levelTypeCode": "L",
"roadNumber1": "810",
"roadName": "HAY",
"roadTypeCode": "ST",
"localityName": "PERTH",
"postcode": "6000",
"stateTerritoryCode": "WA"
}
yaml_string = """
---
- properties:
METHOD: GET
TYPE: ADDRESS
Request URL: /addresses
testCaseId: TC2
request: null
after_request: another value
"""
>>> print(insert_json_in_yaml(yaml_string, 'request', embedded_object))
- properties:
METHOD: GET
TYPE: ADDRESS
Request URL: /addresses
testCaseId: TC2
request: {
"unitTypeCode": "",
"unitNumber": "15",
"levelTypeCode": "L",
"roadNumber1": "810",
"roadName": "HAY",
"roadTypeCode": "ST",
"localityName": "PERTH",
"postcode": "6000",
"stateTerritoryCode": "WA"
}
after_request: another value

i got empty value when print json key in lua code

this is the json response plus "\x00" in the end from server :
{
"STATUS": [{
"STATUS":"S",
"When":1470180059,
"Code":11,
"Msg":"Summary",
"Description":"nsgminer 0.9.2"
}],"SUMMARY": [{
"Elapsed":2061,
"MHS av":0.00,
"Found Blocks":0,
"Getworks":76,
"Accepted":0,
"Rejected":0,
"Hardware Errors":0,
"Utility":0.00,
"Discarded":209,
"Stale":0,
"Get Failures":3,
"Local Work":293,
"Remote Failures":0,
"Network Blocks":14,
"Total MH":0.0000,
"Work Utility":0.00,
"Difficulty Accepted":0.00000000,
"Difficulty Rejected":0.00000000,
"Difficulty Stale":0.00000000,
"Best Share":0
}],
"id":1
}\x00
i want to use the json in lua code :
local output = stdnse.output_table()
local json_string = tostring(result:sub(1, -2))
local pos, value = json.parse(json_string)
output["Description"] = value["STATUS"][0]["Description"]
return output
when i print it out, i got null value
i solve that with covert json to string and convert string into json table
local pos, value = json.parse(tostring(json_string))
output["Description"] = value["STATUS"][1]["Description"]