Skip to content

Add support for 'headers' option #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ doUpload(){
|---|---|---|---|---|
|`url`|string|required|URL to upload to|`http://my.server/api/upload`|
|`method(only iOS)`|string|optional|HTTP method, values: [PUT,POST], default: POST|`POST`|
|`headers(only iOS)`|object|optional|HTTP headers|`{ 'Accept': 'application/json' }`|
|`headers`|object|optional|HTTP headers|`{ 'Accept': 'application/json' }`|
|`params(iOS)`|object|optional|Query parameters|`{ 'user_id': 1 }`|
|`params(Android)`|object|optional|Query parameters|`{ 'user_id': '1' }`<br> only support string value. You can't use int, boolean, etc..|
|`files`|array|required|Array of file objects to upload. See below.| `[{ name: 'file', filename: 'image1.png', filepath: 'assets-library://...', filetype: 'image/png' } ]` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request.Builder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
Expand Down Expand Up @@ -139,8 +140,22 @@ public void onRequestProgress(long bytesWritten, long contentLength) {
}
});

Request request = new Request.Builder()
.header("Accept", "application/json")
Builder builder = new Request.Builder();
if (options.hasKey("headers")) {
ReadableMap data = options.getMap("headers");
ReadableMapKeySetIterator iterator = data.keySetIterator();

while(iterator.hasNextKey()) {
String key = iterator.nextKey();
if(ReadableType.String.equals(data.getType(key))) {
builder.addHeader(key, data.getString(key));
}
}
} else {
builder.addHeader("Accept", "application/json");
}

Request request = builder
.url(url)
.post(monitoredRequest)
.build();
Expand Down