Skip to content
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

feat: Set content-type header for HTTP requests #328

Merged
merged 3 commits into from
Feb 22, 2021
Merged
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
1 change: 1 addition & 0 deletions Sources/Amplitude/AMPConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern NSString *const kAMPPlatform;
extern NSString *const kAMPOSName;
extern NSString *const kAMPEventLogDomain;
extern NSString *const kAMPEventLogUrl;
extern NSString *const kAMPContentTypeHeader;
extern NSString *const kAMPDyanmicConfigUrl;
extern NSString *const kAMPDefaultInstance;
extern const int kAMPApiVersion;
Expand Down
1 change: 1 addition & 0 deletions Sources/Amplitude/AMPConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
NSString *const kAMPUnknownVersion = @"unknown-version";
NSString *const kAMPEventLogDomain = @"api2.amplitude.com";
NSString *const kAMPEventLogUrl = @"https://api2.amplitude.com/";
NSString *const kAMPContentTypeHeader = @"application/x-www-form-urlencoded";
NSString *const kAMPDyanmicConfigUrl = @"https://regionconfig.amplitude.com/";
NSString *const kAMPDefaultInstance = @"$default_instance";
const int kAMPApiVersion = 3;
Expand Down
16 changes: 16 additions & 0 deletions Sources/Amplitude/Amplitude.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ typedef NSDictionary *_Nullable (^AMPLocationInfoBlock)(void);
*/
@property (nonatomic, strong, nullable) AMPLocationInfoBlock locationInfoBlock;

/**
Content-Type header for event sending requests. Only relevant for sending events to a different URL (e.g. proxy server)
*/
@property (nonatomic, copy, readonly) NSString *contentTypeHeader;

#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
/**
Show Amplitude Event Explorer when you're running a debug build.
Expand Down Expand Up @@ -607,8 +612,16 @@ typedef NSDictionary *_Nullable (^AMPLocationInfoBlock)(void);
*/
- (void)disableCoppaControl;

/**
Sends events to a different URL other than kAMPEventLogUrl. Used for proxy servers
*/
- (void)setServerUrl:(NSString *)serverUrl;

/**
Sets Content-Type header for event sending requests
*/
- (void)setContentTypeHeader:(NSString *)contentType;

- (void)setBearerToken:(NSString *)token;

/**-----------------------------------------------------------------------------
Expand Down Expand Up @@ -669,6 +682,9 @@ typedef NSDictionary *_Nullable (^AMPLocationInfoBlock)(void);
*/
- (BOOL)startOrContinueSession:(long long)timestamp;


- (NSString *)getContentTypeHeader;

@end

#pragma mark - constants
Expand Down
12 changes: 11 additions & 1 deletion Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ @interface Amplitude ()
@property (nonatomic, assign) int backoffUploadBatchSize;
@property (nonatomic, copy, readwrite, nullable) NSString *userId;
@property (nonatomic, copy, readwrite) NSString *deviceId;
@property (nonatomic, copy, readwrite) NSString *contentTypeHeader;
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
@property (nonatomic, strong) AMPEventExplorer *eventExplorer;
#endif
Expand Down Expand Up @@ -200,6 +201,7 @@ - (instancetype)initWithInstanceName:(NSString *)instanceName {
_serverUrl = kAMPEventLogUrl;
self.libraryName = kAMPLibrary;
self.libraryVersion = kAMPVersion;
self.contentTypeHeader = kAMPContentTypeHeader;
_inputTrackingOptions = [AMPTrackingOptions options];
_appliedTrackingOptions = [AMPTrackingOptions copyOf:_inputTrackingOptions];
_apiPropertiesTrackingOptions = [NSDictionary dictionary];
Expand Down Expand Up @@ -929,7 +931,7 @@ - (void)makeEventUploadPostRequest:(NSString *)url events:(NSString *)events num
[postData appendData:[checksum dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:self.contentTypeHeader forHTTPHeaderField:@"Content-Type"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you set a default value if it's nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to add that in init method, should be fixed now

[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postData length]] forHTTPHeaderField:@"Content-Length"];

if (_token != nil) {
Expand Down Expand Up @@ -1378,6 +1380,14 @@ - (void)setServerUrl:(NSString *)serverUrl {
self->_serverUrl = serverUrl;
}

- (void)setContentTypeHeader:(NSString *)contentTypeHeader {
self->_contentTypeHeader = contentTypeHeader;
}

- (NSString *)getContentTypeHeader {
return self.contentTypeHeader;
}

- (void)setBearerToken:(NSString *)token {
if (!(token == nil || [self isArgument:token validType:[NSString class] methodName:@"setBearerToken:"])) {
return;
Expand Down