{"id":16829677,"url":"https://github.com/mxcl/omghttpurlrq","last_synced_at":"2025-04-04T09:08:25.913Z","repository":{"id":37381831,"uuid":"21189844","full_name":"mxcl/OMGHTTPURLRQ","owner":"mxcl","description":"Vital extensions to NSURLRequest that Apple left out for some reason.","archived":false,"fork":false,"pushed_at":"2024-02-22T11:50:43.000Z","size":120,"stargazers_count":340,"open_issues_count":3,"forks_count":48,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-10-14T11:35:02.162Z","etag":null,"topics":["omg"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mxcl.png","metadata":{"funding":{"github":"mxcl","patreon":"mxcl"},"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-06-25T04:05:33.000Z","updated_at":"2024-10-01T07:30:43.000Z","dependencies_parsed_at":"2024-06-18T15:26:42.812Z","dependency_job_id":null,"html_url":"https://github.com/mxcl/OMGHTTPURLRQ","commit_stats":{"total_commits":80,"total_committers":16,"mean_commits":5.0,"dds":"0.48750000000000004","last_synced_commit":"d3781339560d47a9e5cb9e70a81dc16b2d291edc"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mxcl%2FOMGHTTPURLRQ","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mxcl%2FOMGHTTPURLRQ/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mxcl%2FOMGHTTPURLRQ/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mxcl%2FOMGHTTPURLRQ/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mxcl","download_url":"https://codeload.github.com/mxcl/OMGHTTPURLRQ/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247149501,"owners_count":20891954,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["omg"],"created_at":"2024-10-13T11:35:06.910Z","updated_at":"2025-04-04T09:08:25.886Z","avatar_url":"https://github.com/mxcl.png","language":"Objective-C","funding_links":["https://github.com/sponsors/mxcl","https://patreon.com/mxcl"],"categories":[],"sub_categories":[],"readme":"# OMGHTTPURLRQ ![Build Status](https://github.com/mxcl/OMGHTTPURLRQ/actions/workflows/CI.yml/badge.svg)\n\n[![Join the chat at https://gitter.im/mxcl/OMGHTTPURLRQ](https://badges.gitter.im/mxcl/OMGHTTPURLRQ.svg)](https://gitter.im/mxcl/OMGHTTPURLRQ?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nVital extensions to `NSURLRequest` that Apple left out for some reason.\n\n```objc\nNSMutableURLRequest *rq = [OMGHTTPURLRQ GET:@\"http://api.com\":@{@\"key\": @\"value\"}];\n\n// application/x-www-form-urlencoded\nNSMutableURLRequest *rq = [OMGHTTPURLRQ POST:@\"http://api.com\":@{@\"key\": @\"value\"}];\n\n// application/json\nNSMutableURLRequest *rq = [OMGHTTPURLRQ POST:@\"http://api.com\" JSON:@{@\"key\": @\"value\"}];\n\n// PUT\nNSMutableURLRequest *rq = [OMGHTTPURLRQ PUT:@\"http://api.com\":@{@\"key\": @\"value\"}];\n\n// DELETE\nNSMutableURLRequest *rq = [OMGHTTPURLRQ DELETE:@\"http://api.com\":@{@\"key\": @\"value\"}];\n```\n\nYou can then pass these to an `NSURLConnection` or `NSURLSession`.\n\n## `multipart/form-data`\n\nOMG! Constructing multipart/form-data for POST requests is complicated, let us do it for you:\n\n```objc\n\nOMGMultipartFormData *multipartFormData = [OMGMultipartFormData new];\n\nNSData *data1 = [NSData dataWithContentsOfFile:@\"myimage1.png\"];\n[multipartFormData addFile:data1 parameterName:@\"file1\" filename:@\"myimage1.png\" contentType:@\"image/png\"];\n\n// Ideally you would not want to re-encode the PNG, but often it is\n// tricky to avoid it.\nUIImage *image2 = [UIImage imageNamed:@\"image2\"];\nNSData *data2 = UIImagePNGRepresentation(image2);\n[multipartFormData addFile:data2 parameterName:@\"file2\" filename:@\"myimage2.png\" contentType:@\"image/png\"];\n\n// SUPER Ideally you would not want to re-encode the JPEG as the process\n// is lossy. If your image comes from the AssetLibrary you *CAN* get the\n// original `NSData`. See stackoverflow.com.\nUIImage *image3 = [UIImage imageNamed:@\"image3\"];\nNSData *data3 = UIImageJPEGRepresentation(image3);\n[multipartFormData addFile:data3 parameterName:@\"file2\" filename:@\"myimage3.jpeg\" contentType:@\"image/jpeg\"];\n\nNSMutableURLRequest *rq = [OMGHTTPURLRQ POST:url:multipartFormData];\n```\n\nNow feed `rq` to `[NSURLConnection sendSynchronousRequest:returningResponse:error:`.\n\n\n## Configuring an `NSURLSessionUploadTask`\n\nIf you need to use `NSURLSession`’s `uploadTask:` but you have become frustrated  because your endpoint expects a multipart/form-data POST request and `NSURLSession` sends the data *raw*, use this:\n\n```objc\nid config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:someID];\nid session = [NSURLSession sessionWithConfiguration:config delegate:someObject delegateQueue:[NSOperationQueue new]];\n\nOMGMultipartFormData *multipartFormData = [OMGMultipartFormData new];\n[multipartFormData addFile:data parameterName:@\"file\" filename:nil contentType:nil];\n\nNSURLRequest *rq = [OMGHTTPURLRQ POST:urlString:multipartFormData];\n\nid path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@\"upload.NSData\"];\n[rq.HTTPBody writeToFile:path atomically:YES];\n\n[[session uploadTaskWithRequest:rq fromFile:[NSURL fileURLWithPath:path]] resume];\n```\n\n\n## OMGUserAgent\n\nIf you just need a sensible UserAgent string for your application you can `pod OMGHTTPURLRQ/UserAgent` and then:\n\n```objc\n#import \u003cOMGHTTPURLRQ/OMGUserAgent.h\u003e\n\nNSString *userAgent = OMGUserAgent();\n```\n\nOMGHTTPURLRQ adds this User-Agent to all requests it generates automatically.\n\nSo for URLRequests generated **other** than by OMGHTTPURLRQ you would do:\n\n```objc\n[someURLRequest addValue:OMGUserAgent() forHTTPHeaderField:@\"User-Agent\"];\n```\n\n\n# Twitter Reverse Auth\n\nYou need an OAuth library, here we use the [TDOAuth](https://github.com/tweetdeck/TDOAuth) pod. You also need\nyour API keys that registering at https://dev.twitter.com will provide\nyou.\n\n```objc\nNSMutableURLRequest *rq = [TDOAuth URLRequestForPath:@\"/oauth/request_token\" POSTParameters:@{@\"x_auth_mode\" : @\"reverse_auth\"} host:@\"api.twitter.com\" consumerKey:APIKey consumerSecret:APISecret accessToken:nil tokenSecret:nil];\n[rq addValue:OMGUserAgent() forHTTPHeaderField:@\"User-Agent\"];\n\n[NSURLConnection sendAsynchronousRequest:rq queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {\n    id oauth = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];\n    SLRequest *reverseAuth = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@\"https://api.twitter.com/oauth/access_token\"] parameters:@{\n        @\"x_reverse_auth_target\": APIKey,\n        @\"x_reverse_auth_parameters\": oauth\n    }];\n    reverseAuth.account = account;\n    [reverseAuth performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *urlResponse, NSError *error) {\n        id creds = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];\n        id credsDict = [NSMutableDictionary new];\n        for (__strong id pair in [creds componentsSeparatedByString:@\"\u0026\"]) {\n            pair = [pair componentsSeparatedByString:@\"=\"];\n            credsDict[pair[0]] = pair[1];\n        }\n        NSLog(@\"%@\", credsDict);\n    }];\n}];\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmxcl%2Fomghttpurlrq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmxcl%2Fomghttpurlrq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmxcl%2Fomghttpurlrq/lists"}