mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-23 11:03:39 +00:00
Better support for python json
This commit is contained in:
@@ -366,7 +366,7 @@ describe('python code sample generator', () => {
|
||||
const output = generator?.generate(input);
|
||||
|
||||
expect(output).toBe(
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/x-www-form-urlencoded"},\n json={"key":"value"}\n)\n\ndata = response.json()'
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/x-www-form-urlencoded"},\n data={"key":"value"}\n)\n\ndata = response.json()'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -402,7 +402,7 @@ describe('python code sample generator', () => {
|
||||
const output = generator?.generate(input);
|
||||
|
||||
expect(output).toBe(
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/xml"},\n json="<key>value</key>"\n)\n\ndata = response.json()'
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/xml"},\n data="<key>value</key>"\n)\n\ndata = response.json()'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -419,7 +419,7 @@ describe('python code sample generator', () => {
|
||||
const output = generator?.generate(input);
|
||||
|
||||
expect(output).toBe(
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/graphql"},\n json="{ key }"\n)\n\ndata = response.json()'
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/json"},\n json="{ key }"\n)\n\ndata = response.json()'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -436,7 +436,7 @@ describe('python code sample generator', () => {
|
||||
const output = generator?.generate(input);
|
||||
|
||||
expect(output).toBe(
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/csv"},\n json="key,value"\n)\n\ndata = response.json()'
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/csv"},\n data="key,value"\n)\n\ndata = response.json()'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -470,7 +470,7 @@ describe('python code sample generator', () => {
|
||||
const output = generator?.generate(input);
|
||||
|
||||
expect(output).toBe(
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/plain"},\n json="value"\n)\n\ndata = response.json()'
|
||||
'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/plain"},\n data="value"\n)\n\ndata = response.json()'
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -126,11 +126,16 @@ export const codeSampleGenerators: CodeSampleGenerator[] = [
|
||||
code += indent(`headers=${stringifyOpenAPI(headers)},\n`, 4);
|
||||
}
|
||||
|
||||
const contentType = headers?.['Content-Type'] || '';
|
||||
|
||||
if (body) {
|
||||
if (body === 'files') {
|
||||
code += indent(`files=${body}\n`, 4);
|
||||
} else {
|
||||
} else if (contentType === 'application/json') {
|
||||
// If the content type is JSON, we use json={}
|
||||
code += indent(`json=${stringifyOpenAPI(body)}\n`, 4);
|
||||
} else {
|
||||
code += indent(`data=${stringifyOpenAPI(body)}\n`, 4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,7 +331,9 @@ const BodyGenerators = {
|
||||
getPythonBody: (body: any, headers?: Record<string, string>) => {
|
||||
if (!body || !headers) return;
|
||||
let code = '';
|
||||
const contentType: string = headers['Content-Type'] || '';
|
||||
// Copy headers to avoid mutating the original object
|
||||
const headersCopy = { ...headers };
|
||||
const contentType: string = headersCopy['Content-Type'] || '';
|
||||
|
||||
if (isFormData(contentType)) {
|
||||
code += 'files = {\n';
|
||||
@@ -346,7 +353,12 @@ const BodyGenerators = {
|
||||
body = 'files';
|
||||
}
|
||||
|
||||
return { body, code, headers };
|
||||
if (isGraphQL(contentType)) {
|
||||
// Set Content-Type to application/json for GraphQL, recommended by GraphQL spec
|
||||
headersCopy['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
return { body, code, headers: headersCopy };
|
||||
},
|
||||
getHTTPBody: (body: any, headers?: Record<string, string>) => {
|
||||
if (!body || !headers) return undefined;
|
||||
|
||||
Reference in New Issue
Block a user