Skip to content

Commit 923097b

Browse files
committed
fix bug where Content-Type header would be overriden to text/plain if set manually with request body
1 parent cdc4b71 commit 923097b

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

__tests__/config/scrape.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ Deno.test('scrapeconfig POST/PUT/PATCH body defaults as content-type text/plain'
6565
assertEquals(config.body, 'foo+bar');
6666
});
6767

68+
Deno.test('scrapeconfig POST/PUT/PATCH body does not override content-type when set', async () => {
69+
const config = new ScrapeConfig({
70+
url: 'http://httpbin.dev/get',
71+
method: 'POST',
72+
body: 'foo+bar',
73+
headers: { 'content-type': 'application/json' },
74+
});
75+
assertEquals((config.headers || {})['content-type'], 'application/json');
76+
assertEquals(config.body, 'foo+bar');
77+
});
78+
6879
Deno.test('scrapeconfig POST/PUT/PATCH data encodes when formdata content-type is set', async () => {
6980
const config = new ScrapeConfig({
7081
url: 'http://httpbin.dev/get',

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
},
55
"name": "@scrapfly/scrapfly-sdk",
66
"exports": "./src/main.ts",
7-
"version": "0.6.7",
7+
"version": "0.6.8",
88
"description": "SDK for Scrapfly.io API for web scraping, screenshotting and data extraction",
99
"tasks": {
1010
"start": "deno run --allow-net --allow-read src/main.ts",

src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ export class ScrapflyClient {
122122
content = await response.text();
123123
result.format = 'text';
124124
}
125-
125+
126126
if (format === 'blob') {
127127
content = new Uint8Array(await response.arrayBuffer());
128128
result.format = 'binary';
129129
}
130-
130+
131131
result.content = content;
132132
return result;
133133
}

src/extractionconfig.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ export class ExtractionConfig {
4545

4646
constructor(options: ExtractionConfigOptions) {
4747
this.validateOptions(options);
48-
if (options.document_compression_format && !Object.values(CompressionFormat).includes(options.document_compression_format as CompressionFormat)) {
49-
throw new errors.ExtractionConfigError(`Invalid CompressionFormat param value: ${options.document_compression_format}`);
50-
}
48+
if (
49+
options.document_compression_format &&
50+
!Object.values(CompressionFormat).includes(options.document_compression_format as CompressionFormat)
51+
) {
52+
throw new errors.ExtractionConfigError(
53+
`Invalid CompressionFormat param value: ${options.document_compression_format}`,
54+
);
55+
}
5156
this.body = options.body;
5257
this.content_type = options.content_type;
5358
this.url = options.url ?? this.url;

src/scrapeconfig.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export enum Format {
3838
export enum FormatOption {
3939
NO_LINKS = 'no_links',
4040
NO_IMAGES = 'no_images',
41-
ONLY_CONTENT = 'only_content'
41+
ONLY_CONTENT = 'only_content',
4242
}
4343

4444
type ScrapeConfigOptions = {
@@ -70,7 +70,8 @@ type ScrapeConfigOptions = {
7070
rendering_wait?: number;
7171
wait_for_selector?: string;
7272
screenshots?: Rec<any>;
73-
screenshot_flags?: ('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | 'high_quality' | ScreenshotFlags)[];
73+
screenshot_flags?:
74+
('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | 'high_quality' | ScreenshotFlags)[];
7475
session_sticky_proxy?: boolean;
7576
webhook?: string;
7677
timeout?: number;
@@ -113,7 +114,8 @@ export class ScrapeConfig {
113114
wait_for_selector?: string;
114115
session_sticky_proxy = false;
115116
screenshots?: Rec<any>;
116-
screenshot_flags?: ('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | 'high_quality' | ScreenshotFlags)[];
117+
screenshot_flags?:
118+
('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | 'high_quality' | ScreenshotFlags)[];
117119
webhook?: string;
118120
timeout?: number; // in milliseconds
119121
js_scenario?: Rec<any>;
@@ -202,7 +204,7 @@ export class ScrapeConfig {
202204
);
203205
}
204206
}
205-
} else if (this.body && !this.data) {
207+
} else if (this.body && !this.data && !this.headers['content-type']) {
206208
this.headers['content-type'] = 'text/plain';
207209
}
208210
}

src/screenshotconfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export enum Format {
3434

3535
type ScreenshotConfigOptions = {
3636
url: string;
37-
format?: 'jpg'| 'png' | 'webp'| 'gif' | Format;
37+
format?: 'jpg' | 'png' | 'webp' | 'gif' | Format;
3838
capture?: string;
3939
resolution?: string;
4040
country?: string;
@@ -52,7 +52,7 @@ type ScreenshotConfigOptions = {
5252

5353
export class ScreenshotConfig {
5454
url: string;
55-
format?: 'jpg'| 'png' | 'webp'| 'gif' | Format;
55+
format?: 'jpg' | 'png' | 'webp' | 'gif' | Format;
5656
capture?: string;
5757
resolution?: string;
5858
country?: string = undefined;
@@ -79,7 +79,7 @@ export class ScreenshotConfig {
7979
throw new ScreenshotConfigError(`Invalid Options param value: ${opt}`);
8080
}
8181
});
82-
}
82+
}
8383
this.url = options.url;
8484
this.format = options.format ?? this.format;
8585
this.capture = options.capture ?? this.capture;

0 commit comments

Comments
 (0)