Skip to content

Need a way to add HTTP Headers #877

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

Closed
conanoc opened this issue Mar 30, 2016 · 24 comments
Closed

Need a way to add HTTP Headers #877

conanoc opened this issue Mar 30, 2016 · 24 comments
Labels
type:feature New feature or improvement of existing feature

Comments

@conanoc
Copy link

conanoc commented Mar 30, 2016

I need a way to add custom HTTP headers to parse http requests.
Parse android SDK has an API to add "network interceptors" which can modify request object (or response object either).

It will be good if I can add or remove headers at any point before making requests. I can only set network interceptors at initialization time on android SDK.

@nlutsenko
Copy link
Contributor

This sounds like a good feature request.
Network interceptors sounds like a little bit of overkill, because at that point you might as well fork the SDK and add that functionality.

In terms of headers, let me ask you few questions:

  • Would you set the http headers once at the initialization time of the SDK or per every request is a requirement?
  • What is your use case for the headers? Purely so we can understand it better and maybe extend more portions that just this, or suggest an alternative solution...

@conanoc
Copy link
Author

conanoc commented Mar 31, 2016

Okay. Let me add some information.
I need additional http headers for authentication. The parse server will be placed inside of a kind of gateway and that gateway requires some headers.

The required headers are dependent to the user, so the headers could be changed. Not have to be changed per every request but can be changed at some point after the initialization time.

@nlutsenko
Copy link
Contributor

This is challenging, though we probably can find a way to make this work.
You can definitely get this working using OSS fork of the SDK and exposing/changing PFURLSessionCommandRunner. Let me know if that solution works for you.

@conanoc
Copy link
Author

conanoc commented Apr 8, 2016

Yes. If the commandRunner instance is accessible and it provides an API to set headers, it will be enough.

@nlutsenko
Copy link
Contributor

Would a fork work in your case then?

@conanoc
Copy link
Author

conanoc commented Apr 8, 2016

I'm not sure how to get the instance of PFURLSessionCommandRunner.

@conanoc
Copy link
Author

conanoc commented Apr 26, 2016

I found a way to get the instance of PFURLSessionCommandRunner.

commandRunner = [[ParseManager currentParseManager_] commandRunner];

But that commandRunner is a singleton and has fixed headers already set. So I will not be able to change the headers later.

I think the following way might be possible.

  1. add a setter in PFCommandURLRequestConstructor to set additional headers.
  2. change _getURLRequestHeadersAsyncForCommand: to include the headers.

I think I could get the PFCommandURLRequestConstructor by [commandRunner __requestConstructor]
@nlutsenko what do you think? I hope It can be done without fork.

@janajri
Copy link

janajri commented May 5, 2016

Bump, @conanoc were you able to find a work around for this?

@jcnm
Copy link

jcnm commented May 5, 2016

This could be paired/a response to the parse-server's issue #1059 ?

@conanoc
Copy link
Author

conanoc commented May 10, 2016

@jakeblakeley not yet.

@conanoc
Copy link
Author

conanoc commented May 20, 2016

I end up use swizzling as an adhoc way.
It seems to work well and I could add headers without fork. Lynk and LynkRequestConstructor are classes in my app.

@implementation LynkRequestConstructor

///--------------------------------------
#pragma mark - Public
///--------------------------------------

+ (NSMutableURLRequest *)urlRequestWithURL:(NSURL *)url
                                httpMethod:(NSString *)httpMethod
                               httpHeaders:(NSDictionary *)httpHeaders
                                parameters:(NSDictionary *)parameters {
    NSDictionary *additionalHeaders = [[Lynk sharedInstance] neoidHeaders];
    NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithDictionary:httpHeaders];
    [headers addEntriesFromDictionary:additionalHeaders];

    return [LynkRequestConstructor urlRequestWithURL:url httpMethod:httpMethod httpHeaders:headers parameters:parameters];
}

+ (void) swizzleMethod {
    SEL selector = @selector(urlRequestWithURL:httpMethod:httpHeaders:parameters:);
    Method originalMethod = class_getClassMethod(NSClassFromString(@"PFHTTPURLRequestConstructor") , selector);
    Method swizzledMethod = class_getClassMethod([self class] , selector);
    method_exchangeImplementations(originalMethod, swizzledMethod);
}

@end

@seejayuu
Copy link

I am trying to get Gzip working for all parse requests. I enabled and tested compression on my heroku-hosted Parse instance and all is good. However, my iOS app isn't setting Accept Encoding: Gzip. Am I doing something stupid or is this another instance where custom headers would help?

@richardjrossiii
Copy link
Contributor

@seejayuu I remember testing this a long time ago (GZIP is enabled on all Parse.com responses over 1KB IIRC), and it seemed like NSURLConnection which we were using at the time automatically inserted that header.

It's possible that NSURLSession no longer shares that behavior, which would be quite odd, and a separate bug in its own regard.

@conanoc
Copy link
Author

conanoc commented May 27, 2016

@seejayuu NSURLSession also sends Accept Encoding: Gzip header by defaults. You don't have to do anything to enable gzip in your ios app. You could see your packets gzipped if you capture them by packet sniffers.

@seejayuu
Copy link

I found the problem. The node.js/express migration Parse version that I am hosting on Heroku does not enable gzip on the server side. I corrected that and now all is good.

On May 26, 2016, at 10:18 PM, conanoc [email protected] wrote:

@seejayuu https://github.com/seejayuu NSURLSession also sends Accept Encoding: Gzip header by defaults. You don't have to do anything to enable gzip in your ios app. You could see your packets gzipped if you capture them by packet sniffers.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #877 (comment), or mute the thread https://github.com/notifications/unsubscribe/ACprTGYFocLoQGzlz4S1YqgcIxEBCFZwks5qFlSJgaJpZM4H7VFX.

@swami701
Copy link

swami701 commented Jun 8, 2016

Hi Guys
I ended up doing like the below for android. It worked.

Parse.initialize(new Parse.Configuration.Builder(this).server(Config.PARSE_SERVER_URL)
          .applicationId(Config.PARSE_APPLICATION_ID)
          .clientKey("")
          .addNetworkInterceptor(chain -> {
            ParseHttpRequest request = chain.getRequest();
            ParseHttpRequest.Builder builder =
                new ParseHttpRequest.Builder(request).addHeader(Config.HEADER_KEY,
                    Config.HEADER_VALUE);
            return chain.proceed(builder.build());
          })
          .enableLocalDataStore()
          .build());

But I don't know how to do in iOS. Please help if any of you has the answer.

Thank you.

@conanoc
Copy link
Author

conanoc commented Jun 16, 2016

@swami701 Have you tried the above code (LynkRequestConstructor) that I had put as a comment? Swizzling can be used to intercept something.

@lukelan
Copy link

lukelan commented Jul 27, 2016

@seejayuu & @swami701 : can please share how to enable gzip at parse server side.
Thank you

@seejayuu
Copy link

In lib/cli/parse-server, add:

app.use(require(‘compression’));

before:

app.use(options.mountPath, api);

On Jul 27, 2016, at 6:45 AM, lukelan [email protected] wrote:

@seejayuu https://github.com/seejayuu & @swami701 https://github.com/swami701 : can please share how to enable gzip at parse server side.
Thank you


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #877 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/ACprTG11WEKcBI3abLDeoS8rMkmVf9iTks5qZza4gaJpZM4H7VFX.

@lukelan
Copy link

lukelan commented Jul 27, 2016

Thank @seejayuu for your answer.

I still got the error respond "incorrect header check" when I do REST API request with Content-Encoding = gzip.

Please help to share how you enable it in your server.

"I found the problem. The node.js/express migration Parse version that I am hosting on Heroku does not enable gzip on the server side. I corrected that and now all is good."

Thank again!!

@lukelan
Copy link

lukelan commented Aug 10, 2016

I end up using cloudFlare, it is support gzip. thank all

@wooiliang
Copy link

Hi @conanoc , can you please provide the sample code how to use LynkRequestConstructor in initializing the parse? Thanks! :bowtie:

@conanoc
Copy link
Author

conanoc commented Oct 21, 2016

@wooiliang Just call [LynkRequestConstructor swizzleMethod]; and it should be called only once.

@stale
Copy link

stale bot commented Sep 19, 2018

This issue has been automatically marked as stale because it has not had recent activity. If you believe it should stay open, please let us know! As always, we encourage contributions, check out the Contributing Guide

@mtrezza mtrezza added type:feature New feature or improvement of existing feature and removed type:improvement labels Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

No branches or pull requests

10 participants