handle charset=UTF-8 in Content-Type header (#356)

This commit is contained in:
Boris Verkhovskiy
2022-03-21 08:47:37 -07:00
committed by GitHub
parent 4129d6b885
commit 7595e8b94f
2 changed files with 58 additions and 5 deletions

View File

@@ -46,6 +46,54 @@ params = {
'AC': '1',
}
data = '{"__type":"CreateItemJsonRequest:#Exchange","Header":{"__type":"JsonRequestHeaders:#Exchange","RequestServerVersion":"Exchange2013","TimeZoneContext":{"__type":"TimeZoneContext:#Exchange","TimeZoneDefinition":{"__type":"TimeZoneDefinitionType:#Exchange","Id":"France Standard Time"}}},"Body":{"__type":"CreateItemRequest:#Exchange","Items":[{"__type":"Message:#Exchange","Subject":"API","Body":{"__type":"BodyContentType:#Exchange","BodyType":"HTML","Value":"<html><head><meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=UTF-8\\"><style type=\\"text/css\\" style=\\"display:none\\"><!-- p { margin-top: 0px; margin-bottom: 0px; }--></style></head><body dir=\\"ltr\\" style=\\"font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;\\"><p>API Test for NickC<br></p></body></html>"},"Importance":"Normal","From":null,"ToRecipients":[{"Name":"George LUCAS","EmailAddress":"George.LUCAS@nih.mail.edu.fr","RoutingType":"SMTP","MailboxType":"Mailbox","OriginalDisplayName":"George.LUCAS@nih.mail.edu.fr","SipUri":" "}],"CcRecipients":[],"BccRecipients":[],"Sensitivity":"Normal","IsDeliveryReceiptRequested":false,"IsReadReceiptRequested":false}],"ClientSupportsIrm":true,"OutboundCharset":"AutoDetect","MessageDisposition":"SendAndSaveCopy","ComposeOperation":"newMail"}}'
json_data = {
'__type': 'CreateItemJsonRequest:#Exchange',
'Header': {
'__type': 'JsonRequestHeaders:#Exchange',
'RequestServerVersion': 'Exchange2013',
'TimeZoneContext': {
'__type': 'TimeZoneContext:#Exchange',
'TimeZoneDefinition': {
'__type': 'TimeZoneDefinitionType:#Exchange',
'Id': 'France Standard Time',
},
},
},
'Body': {
'__type': 'CreateItemRequest:#Exchange',
'Items': [
{
'__type': 'Message:#Exchange',
'Subject': 'API',
'Body': {
'__type': 'BodyContentType:#Exchange',
'BodyType': 'HTML',
'Value': '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style type="text/css" style="display:none"><!-- p { margin-top: 0px; margin-bottom: 0px; }--></style></head><body dir="ltr" style="font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;"><p>API Test for NickC<br></p></body></html>',
},
'Importance': 'Normal',
'From': None,
'ToRecipients': [
{
'Name': 'George LUCAS',
'EmailAddress': 'George.LUCAS@nih.mail.edu.fr',
'RoutingType': 'SMTP',
'MailboxType': 'Mailbox',
'OriginalDisplayName': 'George.LUCAS@nih.mail.edu.fr',
'SipUri': ' ',
},
],
'CcRecipients': [],
'BccRecipients': [],
'Sensitivity': 'Normal',
'IsDeliveryReceiptRequested': False,
'IsReadReceiptRequested': False,
},
],
'ClientSupportsIrm': True,
'OutboundCharset': 'AutoDetect',
'MessageDisposition': 'SendAndSaveCopy',
'ComposeOperation': 'newMail',
},
}
response = requests.post('https://localhost/api/service.svc', headers=headers, params=params, cookies=cookies, data=data)
response = requests.post('https://localhost/api/service.svc', headers=headers, params=params, cookies=cookies, json=json_data)

View File

@@ -91,7 +91,9 @@ function getDataString (request) {
const dataString = 'data = ' + repr(request.data) + '\n'
if (util.getHeader(request, 'content-type') === 'application/json') {
const isJson = util.hasHeader(request, 'content-type') &&
util.getHeader(request, 'content-type').split(';')[0].trim() === 'application/json'
if (isJson) {
try {
const dataAsJson = JSON.parse(request.data)
// TODO: we actually want to know how it's serialized by
@@ -302,7 +304,7 @@ export const _toPython = request => {
[dataString, jsonDataString, jsonDataStringRoundtrips] = getDataString(request)
// Remove "Content-Type" from the headers dict
// because Requests adds it automatically when you use json=
if (jsonDataString) {
if (jsonDataString && util.getHeader(request, 'content-type').trim() === 'application/json') {
commentedOutHeaders['content-type'] = 'Already added when you pass json='
if (!jsonDataStringRoundtrips) {
commentedOutHeaders['content-type'] += ' but not when you pass data='
@@ -314,7 +316,10 @@ export const _toPython = request => {
// If you manually pass a Content-Type header it won't set a `boundary`
// wheras curl does, so the request will fail.
// https://github.com/curlconverter/curlconverter/issues/248
if (filesString && util.getHeader(request, 'content-type') === 'multipart/form-data') {
if (filesString &&
util.hasHeader(request, 'content-type') &&
util.getHeader(request, 'content-type').trim() === 'multipart/form-data' &&
!util.getHeader(request, 'content-type').includes('boundary=')) {
// TODO: better wording
commentedOutHeaders['content-type'] = "requests won't add a boundary if this header is set when you pass files="
}