Skip to content

Commit 738d837

Browse files
authored
Merge pull request #37 from extism/fix-http-call
fix: Fixes HTTP Call code
2 parents 60628c8 + 00c290e commit 738d837

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ members = [
66
]
77

88
[workspace.package]
9-
version = "1.0.0-rc1"
9+
version = "1.0.0-rc2"
1010
edition = "2021"
1111
authors = ["The Extism Authors"]
1212
license = "BSD-Clause-3"

crates/core/src/globals.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{borrow::Cow, collections::HashMap, str::from_utf8};
22

3-
use anyhow::{anyhow, bail};
3+
use anyhow::{anyhow, bail, Context};
44
use extism_pdk::extism::load_input;
55
use extism_pdk::*;
66
use quickjs_wasm_rs::{JSContextRef, JSError, JSValue, JSValueRef};
@@ -174,7 +174,7 @@ fn build_http_object(context: &JSContextRef) -> anyhow::Result<JSValueRef> {
174174

175175
let url = req
176176
.get_property("url")
177-
.expect("Http Request should have url property");
177+
.context("Http Request should have url property")?;
178178

179179
let method = req.get_property("method");
180180
let method_str = match method {
@@ -184,35 +184,35 @@ fn build_http_object(context: &JSContextRef) -> anyhow::Result<JSValueRef> {
184184

185185
let mut http_req = HttpRequest::new(url.as_str()?).with_method(method_str);
186186

187-
let headers = req.get_property("headers");
188-
if let Ok(headers) = headers {
187+
let headers = req.get_property("headers")?;
188+
if !headers.is_null_or_undefined() {
189189
if !headers.is_object() {
190190
bail!("Expected headers to be an object");
191191
}
192-
let mut header_values = headers.properties()?;
193-
loop {
194-
let key = header_values.next_key()?;
195-
match key {
196-
None => break,
197-
Some(key) => {
198-
let key = key.as_str()?;
199-
let value = header_values.next_value()?;
200-
let value = value.as_str()?;
201-
http_req.headers.insert(key.to_string(), value.to_string());
192+
if !headers.is_object() {
193+
let mut header_values = headers.properties()?;
194+
loop {
195+
let key = header_values.next_key()?;
196+
match key {
197+
None => break,
198+
Some(key) => {
199+
let key = key.as_str()?;
200+
let value = header_values.next_value()?;
201+
let value = value.as_str()?;
202+
http_req.headers.insert(key.to_string(), value.to_string());
203+
}
202204
}
203205
}
204206
}
205207
}
206208

207-
let body_arg = args.get(1).ok_or(ctx.null_value()?).unwrap();
209+
let body_arg = args.get(1);
208210
let mut http_body: Option<String> = None;
209-
if !body_arg.is_null_or_undefined() {
210-
let body_arg = body_arg.as_str()?;
211-
http_body = Some(body_arg.to_string());
211+
if let Some(body) = body_arg {
212+
http_body = Some(body.as_str()?.to_string());
212213
}
213214

214215
let resp = http::request::<String>(&http_req, http_body)?;
215-
216216
let body = resp.body();
217217
let body = from_utf8(&body)?;
218218

examples/simple_js/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ function greet() {
66
Host.outputString(`Hello, ${Host.inputString()}`)
77
}
88

9-
module.exports = { greet }
9+
module.exports = { greet }

install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ case "$ARCH" in
1515
esac
1616

1717

18-
export TAG="v1.0.0-rc1"
18+
export TAG="v1.0.0-rc2"
1919
curl -L -O "https://github.com/extism/js-pdk/releases/download/$TAG/extism-js-$ARCH-$OS-$TAG.gz"
2020
gunzip extism-js*.gz
2121
sudo mv extism-js-* /usr/local/bin/extism-js

0 commit comments

Comments
 (0)