Skip to content

Commit 4d39b01

Browse files
author
Andreas Guenther
committed
Adding custom header in order to provide a POST with a application/x-www-form-urlencoded MIME type.
1 parent c54f193 commit 4d39b01

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

Sources/OAuth2Client/NXOAuth2Client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
6565
@property (nonatomic, copy, readonly) NSString *clientSecret;
6666
@property (nonatomic, copy, readonly) NSString *tokenType;
6767
@property (nonatomic, strong, readwrite) NSDictionary *additionalAuthenticationParameters;
68+
@property (nonatomic, strong, readwrite) NSDictionary *customHeaderFields;
6869

6970
@property (nonatomic, copy) NSSet *desiredScope;
7071
@property (nonatomic, copy) NSString *tokenRequestHTTPMethod; // defaults to POST

Sources/OAuth2Client/NXOAuth2Client.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ - (void)authenticateWithUsername:(NSString *)username password:(NSString *)passw
393393
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
394394
}
395395

396+
if (self.customHeaderFields) {
397+
[tokenRequest setAllHTTPHeaderFields:self.customHeaderFields];
398+
}
399+
396400
authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
397401
requestParameters:parameters
398402
oauthClient:self

Sources/OAuth2Client/NXOAuth2Connection.m

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,34 @@ - (void)applyParameters:(NSDictionary *)parameters onRequest:(NSMutableURLReques
225225
&& [httpMethod caseInsensitiveCompare:@"PUT"] != NSOrderedSame) {
226226
aRequest.URL = [aRequest.URL nxoauth2_URLByAddingParameters:parameters];
227227
} else {
228-
NSInputStream *postBodyStream = [[NXOAuth2PostBodyStream alloc] initWithParameters:parameters];
229228

230-
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", [(NXOAuth2PostBodyStream *)postBodyStream boundary]];
231-
NSString *contentLength = [NSString stringWithFormat:@"%lld", [(NXOAuth2PostBodyStream *)postBodyStream length]];
232-
[aRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
233-
[aRequest setValue:contentLength forHTTPHeaderField:@"Content-Length"];
229+
NSString *contentType = [aRequest valueForHTTPHeaderField:@"Content-Type"];
234230

235-
[aRequest setHTTPBodyStream:postBodyStream];
231+
if (!contentType || [contentType isEqualToString:@"multipart/form-data"]) {
232+
233+
// sends the POST/PUT request as multipart/form-data as default
234+
235+
NSInputStream *postBodyStream = [[NXOAuth2PostBodyStream alloc] initWithParameters:parameters];
236+
237+
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",[(NXOAuth2PostBodyStream *)postBodyStream boundary]];
238+
NSString *contentLength = [NSString stringWithFormat:@"%lld", [(NXOAuth2PostBodyStream *)postBodyStream length]];
239+
[aRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
240+
[aRequest setValue:contentLength forHTTPHeaderField:@"Content-Length"];
241+
242+
[aRequest setHTTPBodyStream:postBodyStream];
243+
244+
}else if ([contentType isEqualToString:@"application/x-www-form-urlencoded"]) {
245+
246+
// sends the POST/PUT request as application/x-www-form-urlencoded
247+
248+
NSString *query = [[aRequest.URL nxoauth2_URLByAddingParameters:parameters] query];
249+
[aRequest setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];
250+
251+
}
252+
236253
}
237254
}
238255

239-
240256
- (BOOL)trustsAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
241257
forHostname:(NSString *)hostname
242258
withTrustMode:(NXOAuth2TrustMode)trustMode;

0 commit comments

Comments
 (0)