# Posting xml to create new record

**URL:** <https://www.ruby-forum.com/t/posting-xml-to-create-new-record/131852>\
**Category:** Rails\
**Created:** [March 13, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/posting-xml-to-create-new-record/131852 "2008-03-13T21:10:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [March 13, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/posting-xml-to-create-new-record/131852/1 "2008-03-13T21:10:38Z")

</div>

Hello,

I noticed that with the latest version of Rails, scaffolding in the  
controller already has resourceful methods. Based on the comments in  
the code, it seems that all of this is built in, but I’m having some  
trouble with creating new records by posting xml data.

I have a SubversionRepository model and SubversionRepositories  
controller. When I access /subversion\_repositories.xml I get a list of  
repositories in a format like:

\<?xml version="1.0" encoding="UTF-8"?\> ... repo1 group1 ... 

Based on this format, I tried posting a new repository to the address  
/subversion\_repositories.xml using the following xml.

 newrepo newgroup

I store this in the file repo.xml and post it using the following code.

require ‘net/http’  
require ‘uri’  
require ‘rexml/document’

file = File.new “repo.xml”  
xml\_document = REXML::Document.new(file)

puts “sending XML in http POST”  
http\_client = Net::HTTP.new(‘localhost’, 3000)  
http\_result = http\_client.post(‘/subversion\_repositories.xml’,  
xml\_document.to\_s)  
puts "result type: " + http\_result.class.name  
puts "result: " + http\_result.body.to\_s

The script returns the following output.

sending XML in http POST  
result type: Net::HTTPClientError  
result: \<?xml version="1.0" encoding="UTF-8"?\>  
  
Name can’t be blank  
Group can’t be blank

The log file has an entry similar to the following.

Processing SubversionRepositoriesController#create (for XXXXX at  
2008-03-13 14:54:00) [POST]  
Session ID: XXXXX  
Parameters: {“format”=\>“xml”, “action”=\>“create”,  
“controller”=\>“subversion\_repositories”, “\n  
newrepo\n newgroup\n \<created-at  
type”=\>“‘datetime’ nil=‘true’/\>\n 2008-03-12T16:15:51-05:00\n  
”}  
Completed in 0.01600 (62 reqs/sec) | Rendering: 0.00000 (0%) | DB:  
0.00000 (0%) | 422 Unprocessable Entity  
[[http://localhost/subversion\_repositories.xml](http://localhost/subversion_repositories.xml)]

Does anyone know what I’m doing wrong? Does this functionality not come  
built in?

Thanks for your help.  
Justin

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [March 14, 2008, 2:42am UTC](https://www.ruby-forum.com/t/posting-xml-to-create-new-record/131852/2 "2008-03-14T02:42:40Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Have you tried  
> headers = {‘Content-Type’ =\> ‘text/xml’}  
> http\_result = http\_client.post(’/subversion\_repositories.xml’,  
> xml\_document.to\_s,headers)

That is exactly what I needed. Thanks!

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [March 13, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/posting-xml-to-create-new-record/131852/3 "2008-03-13T23:37:22Z")

</div>

Have you tried  
headers = {‘Content-Type’ =\> ‘text/xml’}  
http\_result = http\_client.post(‘/subversion\_repositories.xml’,  
xml\_document.to\_s,headers)

On Mar 13, 1:10 pm, Justin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
