Skip to content

Instantly share code, notes, and snippets.

@samth
Created March 1, 2012 00:28
Show Gist options
  • Select an option

  • Save samth/1946155 to your computer and use it in GitHub Desktop.

Select an option

Save samth/1946155 to your computer and use it in GitHub Desktop.
rudimentary github wrangling
[samth@loki:~ plt] curl -u 'samth:PASSWORD' --data '{"title":"the title"}' https://api.github.com/repos/samth/test-bugs/issues
#lang at-exp racket
(require net/url net/base64 net/uri-codec (planet dherman/json))
;; http-authorization-header : string string -> string
(define (http-authorization-header username password)
(let ([username-bytes (string->bytes/utf-8 username)]
[password-bytes (string->bytes/utf-8 password)])
(string-append
"Authorization: Basic "
(bytes->string/utf-8
(base64-encode
(bytes-append username-bytes #":" password-bytes) #"")))))
(define data ;; works
(string->bytes/utf-8 (jsexpr->json (hash 'title "found a bug"))))
(define data1 ;; works
@(compose string->bytes/utf-8 string-append){{"title":"found a bug","body":"you cannot be serious" }})
(define data2 ;; works
@(compose string->bytes/utf-8 string-append){{"title":"found a bug","body":"you cannot be serious"}})
(define (go d)
(port->string
(post-pure-port
(string->url "https://api.github.com/repos/samth/test-bugs/issues")
d
(list (http-authorization-header "samth" "PASSWD")))))
@elibarzilay

Copy link
Copy Markdown
  • Neither worked for me.
  • The next step is to listen on a port and see the difference between the two. Either use racket or netcat (nc -l 9898).
  • The problem is that the base64 string has a newline in its end. I regexp-replaced it away and both versions work.
  • Matthew extended that recently, so there's an optional argument to base64-encode for the line separator.

@samth

samth commented Mar 1, 2012

Copy link
Copy Markdown
Author

Can you fork this gist with a version that works? And why does the base64 string affect the later newline?

@samth

samth commented Mar 1, 2012

Copy link
Copy Markdown
Author

Note for the future: current gist works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment