Converting a CURL command for Objective C

后端 未结 3 1402
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 05:36

I\'m having a difficulty transforming this curl command for my Objective C Code:

curl -u 0ef106c78146a23becba9105d1e:X -H \"Accept: application/json\" -H \"C         


        
3条回答
  •  清酒与你
    2021-01-19 06:18

    First of all, you don't need the colon in the HTTP header field name.

    Second, you're doing the HTTP authentication wrong. You need to concat your username and password, username:password and Base64 encode the concatenated string data. Use the base64 value as value for the header field Authorization, use the code below and drop in a Base 64 encoding implementation.

        // snip
        NSMutableUrlRequest* theRequest = [NSMutableUrlRequest ...]
        [MyClass httpAuthorizeRequest:theRequest withUsername:@"someuser" andPassword:@"mysecret"];
        // snip
    
    + (void)httpAuthorizeRequest:(NSMutableURLRequest*)request withUsername:(NSString*)username andPassword:(NSString*)password
    {
        NSString* authorizationToken = [[NSString stringWithFormat:@"%@:%@", username, password] base64Representation];
        [request setValue:[NSString stringWithFormat:@"Basic %@", authorizationToken] forHTTPHeaderField:@"Authorization"];
    }
    

提交回复
热议问题